2

I am writing a program in Scheme (Dr. Racket) to verify Canadian postal-codes. The user inputs a postal code and gets a response whether it is valid or not. I got the boolean logic down but I am stumped as to how to actually tell it what the correct format is.

ex. (valid-postal-code? N2L 3G1) => true

How do I do this?

Thanks

Trevor Hodgins
  • 95
  • 1
  • 10
  • 1
    Are you asking a domain-specific question (e.g. what's the structure of a valid Canadian postal code), or are you asking how to program it? It's very hard to tell what the question's about. Wikipedia discusses the structure of Canadian postal codes: http://en.wikipedia.org/wiki/Postal_codes_in_Canada#Components_of_a_postal_code – dyoo Feb 05 '13 at 19:36

2 Answers2

4

If you want to know if a string has the format of a valid postal code, you can use a regular expression. Canadian postal codes consist of six characters, alternating letters and digits beginning with a letter, with a space embedded between the third and fourth characters. A suitable regular expression is ^[A-Z][0-9][A-Z] [0-9][A-Z][0-9]$.

if you want to know if a string with a valid format is on the list of postal codes, the easiest solution is a bloom filter. I provide a bloom filter, written in Scheme, at my blog.

user448810
  • 17,381
  • 4
  • 34
  • 59
0

I don't know how Canadian postal codes work, but I think what you are asking is that you probably just have a long list of valid codes and need to tell the program that they are ok, and no other codes are.

Using a mutable hash map would be ideal for your purpose: http://docs.racket-lang.org/guide/hash-tables.html

aestrivex
  • 5,170
  • 2
  • 27
  • 44