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

```I came to the following result as conversion function :

#lang typed/racket
(: string2value (-> String Integer))
(define (string2value astring)
(define (fun [val : Char] [res : Integer])
(+ (* 10 res) (- (char->integer val) 48)))
(foldl fun 0 (string->list astring))
)
(print (string2value "12345"))

On Tuesday, February 11, 2020 at 11:34:16 AM UTC+1, Alain De Vos 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 email
To view this discussion on the web visit

```

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

```On Tue, Feb 11, 2020 at 3:28 PM Alain De Vos  wrote:

> But first i need a list of characters. :)
> Does the language has a conversion operator ?
>

Yes, `string->list`:
https://docs.racket-lang.org/reference/strings.html#(def._((quote._~23~25kernel)._string-~3elist))

On Tue, Feb 11, 2020 at 3:28 PM Alain De Vos  wrote:

> 0) Given a string,
> 1) convert to  a list of characters,
> 2) allow me to iterate,
> 3) convert a character to an int ,
> 4) subtract corresponding value of zero,
> 5) allow me to some positional stuff and addition.
>

If you really want to implement the algorithm you described, here's one way
to do it:
#lang typed/racket
(: string->integer (-> String Integer))
(define (string->integer str)
(define base (char->integer #\0))
(for/fold ([acc : Integer 0])
([char (in-string str)])
(+ (* 10 acc) (- (char->integer char) base

Again, I actually would do this with `string->number`, as I illustrated
earlier, unless this arithmetic with Unicode scalars is really what you
want to compute. For example, what about "-"?

On Tue, Feb 11, 2020 at 2:15 PM Alain De Vos  wrote:

> But at each step I should raise , this is not ok.
> Otherwise the GUI just keeps eating memory ...
>

I don't understand what you mean here. Raising an exception should not
cause a memory leak. If you mean that it makes the GUI non-responsive, then
you should catch the exception and show a message to the user (or do the

-Philip

--
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 view this discussion on the web visit

```

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

```You can convert a string to a list of characters by using string->list. The
code snippet that you presented in your very first post also uses this
function.

> (string->list "abc")
- : (Listof Char)
'(#\a #\b #\c)

What I want to ask you though is what is wrong with the code that Phillip
suggested? The code is the most standard way to write a function to convert
a string to an integer. Have you tried it?

(: myconversion (-> String Integer))
(define (myconversion str)
(define rslt (string->number str))
(cond
[(exact-integer? rslt)
rslt]
[else
(raise-argument-error 'myconversion
"a string representing an integer"
str)]))

On Tue, Feb 11, 2020 at 12:28 PM Alain De Vos
wrote:

> In C I would would do some very simple pointer arithmetic, but racket
> leaves me into the blue.
> Documentation, which is fine, compared to other lisps, fyi chez, leaves me
> into the blue.
> 0) Given a string,
> 1) convert to  a list of characters,
> 2) allow me to iterate,
> 3) convert a character to an int ,
> 4) subtract corresponding value of zero,
> 5) allow me to some positional stuff and addition.
> But first i need a list of characters. :)
> Does the language has a conversion operator ?
>
> On Tuesday, February 11, 2020 at 8:50:33 PM UTC+1, Alain De Vos wrote:
>>
>> Very basic question, first step first, how do i convert a string astring
>> to a list of characters,
>> #lang typed/racket
>> (require typed/racket/gui)
>> (: astring String)
>> (define astring "1234567890")
>>
>>
>> On Tuesday, February 11, 2020 at 11:34:16 AM UTC+1, Alain De Vos 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
To view this discussion on the web visit

```

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

```In C I would would do some very simple pointer arithmetic, but racket
leaves me into the blue.
Documentation, which is fine, compared to other lisps, fyi chez, leaves me
into the blue.
0) Given a string,
1) convert to  a list of characters,
2) allow me to iterate,
3) convert a character to an int ,
4) subtract corresponding value of zero,
5) allow me to some positional stuff and addition.
But first i need a list of characters. :)
Does the language has a conversion operator ?

On Tuesday, February 11, 2020 at 8:50:33 PM UTC+1, Alain De Vos wrote:
>
> Very basic question, first step first, how do i convert a string astring
> to a list of characters,
> #lang typed/racket
> (require typed/racket/gui)
> (: astring String)
> (define astring "1234567890")
>
>
> On Tuesday, February 11, 2020 at 11:34:16 AM UTC+1, Alain De Vos 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 email
To view this discussion on the web visit

```

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

```Very basic question, first step first, how do i convert a string astring to
a list of characters,
#lang typed/racket
(require typed/racket/gui)
(: astring String)
(define astring "1234567890")

On Tuesday, February 11, 2020 at 11:34:16 AM UTC+1, Alain De Vos 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 email
To view this discussion on the web visit

```

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

```Or is it they idea to write own conversion functions to learn the language.

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