Iterate through the letters of the alphabet in Racket

I would like to write a program that iterates through the letters in the alphabet in the form of characters and does something with them. I would like this to be roughly equivalent to this C code:

for(char letter = 'a'; letter <= 'z'; letter++)
{
    printf("The letter is %c\n", letter);
}

I really don't know how to do this in Racket. Thank you for your help.

+3
source share
2 answers

Assuming you only want to iterate over the lower case letters of the English alphabet, here is one way to do this:

(define alphabet (string->list "abcdefghijklmnopqrstuvwxyz"))

(for ([letter alphabet])
  (displayln letter))

You can do a lot more with loops for. For instance,

(for/list ([let alphabet] [r-let (reverse alphabet)])
  (list let r-let))

creates a list of letters paired with letters going in the other direction. Although it really is better expressed in the form of cards: (map list alphabet (reverse alphabet)).

, SRFI-14 , .

Edit: - char->integer, integer->char range, , , .

+9

, :

#lang racket
(for ([letter (in-range (char->integer #\a)
                        (add1 (char->integer #\z)))])
  (printf "The letter is ~a\n" (integer->char letter)))

Racket , C: C, Racket , . char->integer integer->char .

+7

All Articles