I’d like to write a program that iterates through the letters in the alphabet as symbols and does something with them. I’d like it to be roughly equivalent to this C code:
for(char letter = 'a'; letter <= 'z'; letter++)
{
printf("The letter is %c\n", letter);
}
I really have no idea how to do this in Racket. Thanks for your help.
Assuming that you only want to iterate over lowercase English alphabet letters, here’s one way to do it:
You can do a lot more with
forloops though. For example,produces a list of letters paired with letters going the other direction. Although that’s actually better expressed as a map:
(map list alphabet (reverse alphabet)).Also, SRFI-14 provides more operations over sets of characters if you need more.
Edit: Originally, I did something with
char->integer,integer->char, andrangebut what I have now is simpler.