# Re: [racket-users] How to convert String to Integer

```Indeed good question, but the following code works,

#lang typed/racket
(require typed/racket/gui)```
```
(: fib (-> Integer Integer))
(define (fib n)
(if (< 2 n)
n
(+ (fib (- n 1))
(fib (- n 2)))))

(: numericalchar2integer (-> Char Integer))
(define (numericalchar2integer char)
(let ([num (- (char->integer char) 48)]) ; 48 = (char->integer #\0)
(if
(or (< num 0) (> num 9))
(raise 'non-numerical-char #t)
num)))

(: string2integer (-> String Integer))
(define (string2integer str)
(let ([char-list (string->list str)])
(if (null? char-list)
(raise 'empty-string #t)
(foldl
(λ([x : Integer] [y : Integer])
(+ (* y 10) x))
0
(map numericalchar2integer char-list)))))

And the following code fails , just one the first line difference,
#lang
<https://docs.racket-lang.org/guide/Module_Syntax.html#%28part._hash-lang%29>
sweet-exp <https://docs.racket-lang.org/sweet/index.html> typed/racket
(: fib (-> Integer Integer))
(define (fib n)
(if (< 2 n)
n
(+ (fib (- n 1))
(fib (- n 2)))))

(: numericalchar2integer (-> Char Integer))
(define (numericalchar2integer char)
(let ([num (- (char->integer char) 48)]) ; 48 = (char->integer #\0)
(if
(or (< num 0) (> num 9))
(raise 'non-numerical-char #t)
num)))

(: string2integer (-> String Integer))
(define (string2integer str)
(let ([char-list (string->list str)])
(if (null? char-list)
(raise 'empty-string #t)
(foldl
(λ([x : Integer] [y : Integer])
(+ (* y 10) x))
0
(map numericalchar2integer char-list)))))

Error, unbound identifier y.
Has sweet-exp a problem with lambda functions ?

On Tuesday, February 11, 2020 at 12:02:46 PM UTC+1, Ryan Culpepper wrote:
>
> What should `(myconversion "apple")` return?
> What should `(myconversion "12.3")` return?
> What does `string->number` do in each of those cases?
>
> Ryan
>
>
> On Tue, Feb 11, 2020 at 11:34 AM Alain De Vos <devosa...@gmail.com
> <javascript:>> wrote:
>
>> 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)
>>     (if
>>      (or (< num 0) (> num 9))
>>      (raise 'non-numerical-char #t)
>>      num)))
>> (: string->integer (-> String
>>                        Integer))(define (string->integer str)
>>   (let ([char-list (string->list str)])
>>     (if (null? char-list)
>>         (raise 'empty-string #t)
>>         (foldl
>>          (λ([x : Integer] [y : Integer])
>>            (+ (* y 10) x))
>>          0
>>          (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
>> To view this discussion on the web visit
>>
>> .
>>
>

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