Here is one way of converting a number from its string form to integer form:
(define (string->num string)
(define dict '(("one" 1) ("two" 2) ("three" 3) ("four" 4) ("five" 5)
("six" 6) ("seven" 7) ("eight" 8) ("nine" 9) ("ten" 10)
("eleven" 11) ("twelve" 12) ("thirteen" 13) ("fourteen" 14)
("fifteen" 15) ("sixteen" 16) ("seventeen" 17) ("eighteen" 18)
("ninteen" 19) ("twenty" 20) ("thirty" 30) ("forty" 40)
("fifty" 50) ("sixty" 60) ("seventy" 70) ("eighty" 80)
("ninety" 90)))
(define (get-num str lst)
(cond [(empty? lst) 0]
[(equal? str (first (first lst)))
(second (first lst))]
[else (get-num str (rest lst))]))
(define str-lst (regexp-split #rx"-" string))
(define len (length str-lst))
(define base (get-num (first str-lst) dict))
(cond [(equal? len 1)
base]
[(equal? len 2)
(+ base (get-num (second str-lst) dict))]))
Function uses regexp splitting to separate the string into two non-hyphenated parts, eg. (regexp-split #rx"-" "twenty-four")
returns '("twenty" "four")
, and (regexp-split #rx"-" "one")
returns '("one")
. The idea is to use the dictionary (list-of lists) as a source, and look up the value of the string, if a single string, or look up values of both strings, if there were a hyphen. If there are two strings, their values are added, eg. from '("twenty" "four")
, values of both "twenty" and "four" are looked up, after which they are added to return 24 (20 + 4).