0

How can I convert a string that spells a number (for example "four") to an integer ? In summary I'm trying to change "four" to 4. The range of numbers I need to convert are from 1 - 99. numbers over nine will have a dash (example: twenty-five).

What I have tried, string->number, but this returns false. I'm not sure what else to try.

Please help.

raffi
  • 3
  • 2
  • all i could think of was string->number "four" – raffi Feb 01 '17 at 02:53
  • i can't think of another way to do it other then defining 99 terms – raffi Feb 01 '17 at 02:53
  • It is not as simple as that here is a good answer in python that perhaps you could convert to scheme http://stackoverflow.com/a/493788/6328256 and here is a question that goes over the general algorithm http://stackoverflow.com/questions/70161/how-to-read-values-from-numbers-written-as-words – Sash Sinha Feb 01 '17 at 02:59
  • i'll give it a try but I just started using this programming language and am not very familiar with lists. – raffi Feb 01 '17 at 03:05
  • I haven't had much luck yet but I'm going to keep trying if anyone else has a idea please let me know thanks. – raffi Feb 01 '17 at 03:22
  • string->number converts a string that is similar to a number like "1234" to type integer 1234. it can not convert a string like four to integer 4 – Douglas Hosea Dec 11 '17 at 07:23

2 Answers2

0

Here is an interesting solution:

(define count
  (lambda (limit)
    (let loop ((i 0))
      (let ((number (format #f "~r" i)))
        (if (equal? number limit)
            i
            (loop (+ i 1)))))))
Michael Vehrs
  • 3,293
  • 11
  • 10
0

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).

assefamaru
  • 2,751
  • 2
  • 10
  • 14
  • Hey thanks for the help I also figured out a similar way of doing it but instead of using a list I defined the same set of numbers you did and I also used (con to set the numbers there equal to. Then I switch the string im trying to covert to a number to a symbol and use equal? To compare and if there the same I call the number Ito equal to. But your way is definitely a lot more quick and easier:) – raffi Feb 02 '17 at 16:11