ZetCode

Scheme characters

last modified November 24, 2026 Guile 3.0

A character is a single Unicode code point, the element that strings are made of. The data types article introduced character literals and char->integer, and the strings article worked with whole strings. This article covers the procedures that compare, convert, and classify single characters, and Guile's character sets. The examples run with GNU Guile 3.0.7.

Character literals

A character literal starts with #\, followed by the character itself, by a name, or by x and a hexadecimal code point. Names exist for characters that are invisible or hard to type, such as #\space, #\tab, and #\newline.

literals.scm
(define chars
  (list #\a #\Z #\7 #\( #\space #\tab #\newline #\nul #\delete))

(for-each (lambda (c)
            (write c)
            (display " "))
          chars)
(newline)

(write (list #\x41 #\x3bb #\x20AC))
(newline)
(display (list #\x41 #\x3bb #\x20AC))
(newline)
(write (list #\linefeed #\escape #\alarm))
(newline)

(display "[")
(display #\tab)
(display "]")
(newline)
(write (list (char? #\a) (char? "a") (char? 'a)))
(newline)

write prints characters in their literal syntax, using the name when there is one. #\x41 is the letter A, #\x3bb lambda, and #\x20AC the euro sign; unlike the \x escape inside strings, the character form takes any number of hex digits. Some names are aliases: #\linefeed is written back as #\newline and #\escape as #\esc. display prints the character itself, so a real tab appears between the brackets. A one-character string and a symbol are not characters.

$ guile literals.scm
#\a #\Z #\7 #\( #\space #\tab #\newline #\nul #\delete 
(#\A #\λ #\€)
(A λ €)
(#\newline #\esc #\alarm)
[	]
(#t #f #f)

Character codes

char->integer returns the Unicode code point of a character and integer->char turns a code point back into a character. Code points of consecutive letters are usually consecutive numbers, which makes simple arithmetic on characters possible.

codes.scm
(define (show c)
  (write c)
  (display " ")
  (display (char->integer c))
  (newline))

(for-each show (list #\A #\a #\0 #\Ž #\€ #\x1F600))

(write (integer->char 66))
(display " ")
(write (integer->char 955))
(newline)

(define (char-range from to)
  (map integer->char
       (iota (+ 1 (- (char->integer to) (char->integer from)))
             (char->integer from))))

(display (list->string (char-range #\a #\z)))
(newline)
(display (list->string (char-range #\α #\ω)))
(newline)

The digits and the basic Latin letters have the same codes as in ASCII. Ž, the euro sign, and the emoji lie beyond that range; Guile handles all code points up to #x10FFFF in the same way. char-range converts both ends to integers, uses iota to count from one to the other, and maps the numbers back to characters. The Greek range contains one letter more than the alphabet: ς, the final form of sigma, has its own code point between ρ and σ.

$ guile codes.scm
#\A 65
#\a 97
#\0 48
#\Ž 381
#\€ 8364
#\😀 128512
#\B #\λ
abcdefghijklmnopqrstuvwxyz
αβγδεζηθικλμνξοπρςστυφχψω

Comparing characters

char=?, char<?, char>?, char<=?, and char>=? compare characters by their code points. The -ci variants ignore case. Like the numeric comparisons, they accept more than two arguments.

comparing.scm
(write (list (char=? #\a #\a) (char=? #\a #\A)))
(newline)
(write (list (char<? #\a #\b) (char<? #\Z #\a) (char<? #\9 #\A)))
(newline)
(write (char<? #\a #\b #\c #\d))
(display " ")
(write (char<? #\a #\c #\b))
(newline)
(write (list (char-ci=? #\a #\A) (char-ci<? #\Z #\a)))
(newline)

(define (between? c low high)
  (char<=? low c high))

(write (map (lambda (c) (between? c #\a #\z)) (list #\m #\M #\ž #\5)))
(newline)
(write (list (eqv? #\a #\a) (equal? #\a #\a)))
(newline)

Because of the code points, Z sorts before a, and the digits before all letters. (char<? #\a #\c #\b) is false because the arguments are not in increasing order. char-ci<? compares the lowercase forms, so Z is not smaller than a. between? uses three arguments to char<=? to test a range; ž is outside a–z because its code point is much larger. Characters are compared correctly by eqv? and equal? as well, but the char procedures state the intent and reject other types.

$ guile comparing.scm
(#t #f)
(#t #t #t)
#t #f
(#t #f)
(#t #f #f #f)
(#t #t)

Case

char-upcase and char-downcase convert the case of a letter and return other characters unchanged. char-upper-case? and char-lower-case? test it.

case.scm
(define letters (list #\a #\Q #\ž #\Ω #\5 #\ß))

(write (map char-upcase letters))
(newline)
(write (map char-downcase letters))
(newline)
(write (map char-upper-case? letters))
(newline)
(write (map char-lower-case? letters))
(newline)

(define (swap-case c)
  (cond ((char-upper-case? c) (char-downcase c))
        ((char-lower-case? c) (char-upcase c))
        (else c)))

(display (string-map swap-case "Hello There, 2026!"))
(newline)
(write (list (char-titlecase #\dž) (char-upcase #\dž)))
(newline)

The conversions cover Slovak and Greek letters as well. The digit 5 has no case, so both tests return #f for it. The German ß is a lowercase letter without a single-character uppercase form, so char-upcase leaves it alone. swap-case inverts the case of each letter and is applied to a whole string with string-map. A few characters, such as the digraph dž, have a separate title-case form that differs from the uppercase one.

$ guile case.scm
(#\A #\Q #\Ž #\Ω #\5 #\ß)
(#\a #\q #\ž #\ω #\5 #\ß)
(#f #t #f #t #f #f)
(#t #f #t #f #f #t)
hELLO tHERE, 2026!
(#\Dž #\DŽ)

Classifying characters

char-alphabetic?, char-numeric?, and char-whitespace? test whether a character is a letter, a digit, or white space. They follow the Unicode properties, so they work for any script, not only for ASCII.

classes.scm
(define (describe c)
  (cond ((char-alphabetic? c) "letter")
        ((char-numeric? c) "digit")
        ((char-whitespace? c) "space")
        (else "other")))

(for-each (lambda (c)
            (write c)
            (display " ")
            (display (describe c))
            (newline))
          (list #\k #\Č #\λ #\中 #\4 #\٣ #\space #\tab #\- #\€))

(define text "Room 42, floor 3")

(write (string-count text char-alphabetic?))
(display " ")
(write (string-count text char-numeric?))
(display " ")
(write (string-count text char-whitespace?))
(newline)

Č, λ, and the Chinese character 中 are letters, and the Arabic-Indic digit ٣ (three) is a digit. A tab counts as white space. The hyphen and the euro sign belong to none of the three classes. Guile's string-count accepts a predicate instead of a character, so the last lines count the letters, digits, and spaces of a string directly.

$ guile classes.scm
#\k letter
#\Č letter
#\λ letter
#\中 letter
#\4 digit
#\٣ digit
#\space space
#\tab space
#\- other
#\€ other
9 3 3

Digit values

The digits 0 to 9 have consecutive codes, so the value of a digit is its code minus the code of #\0. This works for ASCII digits, and the program below shows where it stops working.

digits.scm
(define (digit->number c)
  (- (char->integer c) (char->integer #\0)))

(write (map digit->number (string->list "2026")))
(newline)

(define (parse-natural s)
  (let loop ((chars (string->list s)) (value 0))
    (cond ((null? chars) value)
          ((char-numeric? (car chars))
           (loop (cdr chars) (+ (* value 10) (digit->number (car chars)))))
          (else #f))))

(write (list (parse-natural "8080") (parse-natural "80a0")))
(newline)
(write (digit->number #\٣))
(newline)

(define (number->digit n)
  (integer->char (+ n (char->integer #\0))))

(write (list->string (map number->digit '(4 0 4))))
(newline)

digit->number converts each character of "2026", and parse-natural builds a number from the digits of a string, or returns #f when it meets another character. number->digit goes the other way. The subtraction fails for digits from other scripts: char-numeric? accepts ٣, but its code minus 48 is 1587, not 3, so parse-natural would compute nonsense for Arabic-Indic digits.

$ guile digits.scm
(2 0 2 6)
(8080 #f)
1587
"404"

digit-value

R7RS defines digit-value, which returns the value of any decimal digit, or #f for other characters. Guile does not bind it in a plain script, but importing the R7RS library (scheme char) provides it, together with string-foldcase.

digitvalue.scm
(import (scheme char))

(write (map digit-value (list #\0 #\7 #\x #\٣ #\५)))
(newline)

(define (digits-sum s)
  (let loop ((chars (string->list s)) (sum 0))
    (if (null? chars)
        sum
        (let ((d (digit-value (car chars))))
          (loop (cdr chars) (if d (+ sum d) sum))))))

(write (digits-sum "a1b2c3"))
(display " ")
(write (digits-sum "١٢٣"))
(newline)

(write (string-foldcase "Straße"))
(display " ")
(write (string=? (string-foldcase "STRASSE") (string-foldcase "straße")))
(newline)

digit-value knows the ASCII digits, the Arabic-Indic three, and the Devanagari five, and returns #f for the letter x. digits-sum adds the digits of a string and skips everything else; the Arabic-Indic digits ١٢٣ also sum to 6. string-foldcase applies full case folding, which turns ß into ss, so it can compare strings without regard to case even where the one-character conversions cannot.

$ guile digitvalue.scm
(0 7 #f 3 5)
6 6
"strasse" #t

General categories

Unicode assigns each character a general category, a two-letter code such as Lu for uppercase letters or Nd for decimal digits. Guile's char-general-category returns it as a symbol.

categories.scm
(define sample "Aé 5,€+½_")

(string-for-each
 (lambda (c)
   (write c)
   (display " ")
   (display (char-general-category c))
   (newline))
 sample)

(define (punctuation? c)
  (memq (char-general-category c) '(Pc Pd Ps Pe Pi Pf Po)))

(write (filter punctuation? (string->list "Hi, (you) - well_done!")))
(newline)

The first letter of a category gives the main class: L for letters, N for numbers, Z for separators, P for punctuation, S for symbols. The second letter refines it: Sc is a currency symbol, Sm a math symbol, and ½ is No, a number that is not a decimal digit. The underscore is connector punctuation, Pc. punctuation? checks for any of the seven P categories and finds the comma, the parentheses, the dash, the underscore, and the exclamation mark.

$ guile categories.scm
#\A Lu
#\é Ll
#\space Zs
#\5 Nd
#\, Po
#\€ Sc
#\+ Sm
#\½ No
#\_ Pc
(#\, #\( #\) #\- #\_ #\!)

Character sets

A character set is a set of characters with a fast membership test. Guile includes SRFI 14 in its core: char-set creates a set, and predefined sets such as char-set:digit and char-set:letter cover the Unicode classes. Many string procedures accept a character set where they accept a character.

charsets.scm
(define vowels (char-set #\a #\e #\i #\o #\u))

(write (char-set-contains? vowels #\e))
(display " ")
(write (char-set-contains? vowels #\x))
(newline)
(write (char-set-contains? char-set:digit #\٣))
(newline)

(define text "Bratislava, 2026: The Year Ahead!")

(write (string-index text char-set:digit))
(display " ")
(write (string-count text vowels))
(display " ")
(write (string-count text char-set:upper-case))
(newline)
(write (string-delete char-set:punctuation text))
(newline)
(write (string-filter char-set:letter text))
(newline)
(write (char-set->list (char-set-union (char-set #\x #\y) (char-set #\a))))
(newline)

char-set-contains? tests membership, and the predefined digit set includes the Arabic-Indic digits. string-index finds the first digit of the text at index 12, string-count counts the vowels and the uppercase letters, string-delete removes the punctuation, and string-filter keeps only the letters. Sets can be combined with char-set-union and listed with char-set->list, which returns the characters in code point order.

$ guile charsets.scm
#t #f
#t
12 9 4
"Bratislava 2026 The Year Ahead"
"BratislavaTheYearAhead"
(#\a #\x #\y)

A Caesar cipher

The Caesar cipher replaces each letter by the letter a fixed number of places further in the alphabet, wrapping around from z to a. It combines character codes, range tests, and string-map.

caesar.scm
(define (shift-letter c base n)
  (integer->char
   (+ base (modulo (+ (- (char->integer c) base) n) 26))))

(define (shift c n)
  (cond ((char<=? #\a c #\z) (shift-letter c (char->integer #\a) n))
        ((char<=? #\A c #\Z) (shift-letter c (char->integer #\A) n))
        (else c)))

(define (caesar text n)
  (string-map (lambda (c) (shift c n)) text))

(define message "Meet me at the old bridge, 9 PM.")
(define secret (caesar message 3))

(display secret)
(newline)
(display (caesar secret -3))
(newline)
(display (caesar "Hello there" 13))
(newline)
(display (caesar (caesar "Hello there" 13) 13))
(newline)

shift-letter converts a letter to its position from 0 to 25, adds the shift, wraps around with modulo, and converts back. shift chooses the base for lowercase and uppercase letters and leaves every other character unchanged. Because modulo returns a non-negative result for a negative shift, -3 decodes the message. A shift of 13 is its own inverse, which is the ROT13 scheme.

$ guile caesar.scm
Phhw ph dw wkh rog eulgjh, 9 SP.
Meet me at the old bridge, 9 PM.
Uryyb gurer
Hello there

Source

Characters — GNU Guile Reference Manual

Character Sets — GNU Guile Reference Manual

In this article we have written character literals and names, converted between characters and code points, compared characters, changed and tested their case, classified them as letters, digits, and white space, computed digit values by hand and with digit-value, looked up Unicode general categories, worked with character sets, and written a Caesar cipher. The strings article covers the sequences that characters form, and the numbers article explains modulo.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all Scheme tutorials.