Search code examples
postschemeracketpostal-code

Postal Code verification in Scheme (Dr. Racket)


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


Solution

  • 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.