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
for
loops 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
, andrange
but what I have now is simpler.Just so that one of the answers shows the literal translation approach:
Racket doesn't support the implicit punning between characters and integers that C permits: unlike in C, a Racket value's type is intrinsic rather than external to the value itself. The
char->integer
andinteger->char
functions do the explicit translations between those types.