(: 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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/4a13bb2e-3107-4c5f-aee6-6d29e493c040%40googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to racket...@googlegroups.com.
You may want `exact-integer?`
True , i should use exact-integer.
--
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/89f338be-f574-44b5-82d4-23f833ec14ac%40googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to racket...@googlegroups.com.
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)]))
--
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...@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.
But first i need a list of characters. :)
Does the language has a conversion operator ?
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 at each step I should raise , this is not ok.Otherwise the GUI just keeps eating memory ...