Domanda

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

È stato utile?

Soluzione

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.

Altri suggerimenti

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top