I tried the following function to conver a String to an Integer.

#lang typed/racket
(: myconversion (-> String Integer))
(define (myconversion str)
  (string->number str))

The error given is :
Type Checker: type mismatch
  expected: Integer
  given: (U Complex False) in: (string->number str)

I guess this is because a number is not an Integer.

How to proceed?

I found the following code on internet , but this looks like a real 
overkill for this simple problem ,

(: numerical-char->integer (-> Char
                               Integer))(define (numerical-char->integer char)
  (let ([num (- (char->integer char) 48)]) ; 48 = (char->integer #\0)
     (or (< num 0) (> num 9))
     (raise 'non-numerical-char #t)
(: string->integer (-> String
                       Integer))(define (string->integer str)
  (let ([char-list (string->list str)])
    (if (null? char-list)
        (raise 'empty-string #t)
         (λ([x : Integer] [y : Integer])
           (+ (* y 10) x))
         (map numerical-char->integer char-list)))))

You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 

Reply via email to