Factor characters are integers — Unicode code points — so numeric
<, >, = give you case-sensitive ordering directly. The
unicode vocabulary adds Unicode-aware predicates and
single-character case conversion.
LETTER? ( c -- ? ) ! uppercase letter
letter? ( c -- ? ) ! lowercase letter
Letter? ( c -- ? ) ! letter, either case
digit? ( c -- ? ) ! decimal digit
digit> ( c -- n ) ! the integer value of a digit char
blank? ( c -- ? ) ! whitespace
USING: unicode ;
CHAR: A LETTER? . ! => t
CHAR: a letter? . ! => t
CHAR: 7 digit? . ! => t
CHAR: 7 digit> . ! => 7
ch>upper and ch>lower flip a single character. The string-level
>upper and >lower apply them across every character of a string.
CHAR: a ch>upper . ! => 65 (which is CHAR: A)
"Hello" >upper . ! => "HELLO"
capitalize lowercases a whole string and then uppercases its first
character — the title case of a single word:
"hELLO" capitalize . ! => "Hello"
CHAR: A parses to the integer code point of A. For named
characters, use the names space, tab, \n, etc.:
CHAR: A . ! => 65
CHAR: space . ! => 32
CHAR: \n . ! => 10
When a word needs to return a tag rather than a value — say,
yes/no/maybe instead of a boolean — declare each tag as a
symbol. A symbol is a word that pushes itself when called, and
two symbols compare equal only when they are the same name.
USING: kernel ;
SYMBOLS: yes no maybe ;
yes . ! => yes
yes yes = . ! => t
yes no = . ! => f
SYMBOL: name declares one; SYMBOLS: a b c ; declares several.
Heidi, a young Forth Alien, needs to get some work done on her Human alphabets project and asks if you can help her program some words she will need.
Define compare-chars to take two characters and return the symbol
less, greater, or equal depending on whether the first is
less than, greater than, or equal to the second. Compare in a
case-sensitive manner.
CHAR: A CHAR: B compare-chars . ! => less
CHAR: B CHAR: A compare-chars . ! => greater
CHAR: A CHAR: A compare-chars . ! => equal
CHAR: A CHAR: a compare-chars . ! => less
Heidi needs to know whether a character is "big" (uppercase),
"small" (lowercase), or has no size. Define size-of-char to
return big, small, or no-size.
CHAR: A size-of-char . ! => big
CHAR: a size-of-char . ! => small
CHAR: 5 size-of-char . ! => no-size
CHAR: space size-of-char . ! => no-size
Heidi sometimes needs to flip a character to a different size.
Define change-size-of-char to take a character and a desired
size (big or small) and return the corresponding-case
character.
CHAR: a big change-size-of-char . ! => 65 (CHAR: A)
CHAR: A small change-size-of-char . ! => 97 (CHAR: a)
Heidi also needs to know what kind of character it is. Define
type-of-char to return:
alpha for any letter (upper or lower)numeric for a decimal digitspace for the space characternewline for the newline characterunknown otherwiseCHAR: A type-of-char . ! => alpha
CHAR: 5 type-of-char . ! => numeric
CHAR: space type-of-char . ! => space
CHAR: \n type-of-char . ! => newline
CHAR: ! type-of-char . ! => unknown
Sign up to Exercism to learn and master Factor with 47 concepts163 exercises, and real human mentoring, all for free.