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

2020-02-12 Thread Alain De Vos
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 racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/3e58a927-c05a-4688-984c-1750fb014624%40googlegroups.com.


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

2020-02-11 Thread Philip McGrath
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))
But read on …

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
equivalent using a designated "bad value" instead of an exception).

-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 racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/CAH3z3gYQKaKpWmSzWJoiTaHB3HDmrgUrcgH_CZOOhvmYwPV4zQ%40mail.gmail.com.


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

2020-02-11 Thread Sorawee Porncharoenwase
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
> email to racket-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/racket-users/54b2884c-6d93-44ad-b34a-d68c110b73ec%40googlegroups.com
> 
> .
>

-- 
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 
https://groups.google.com/d/msgid/racket-users/CADcuegsw%3DbgNQdodaRiJo%2BW9PmvEDyx-DfCjSHor4UoCTqC4yw%40mail.gmail.com.


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

2020-02-11 Thread Alain De Vos
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 racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/54b2884c-6d93-44ad-b34a-d68c110b73ec%40googlegroups.com.


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

2020-02-11 Thread Alain De Vos
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 racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/2e17ed2c-e1f1-4b56-985c-4202665db250%40googlegroups.com.


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

2020-02-11 Thread Alain De Vos
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 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/fb73a1b4-3aa1-419a-8167-588ac4473316%40googlegroups.com.