Reputation: 35
How can I convert a string of digits to a list of integers ? I want "1234" to be converted to list 1 2 3 4
Upvotes: 2
Views: 1307
Reputation: 48745
You can convert a string to a list of chars with string->list
. Eg.
(string->list "1234") ; ==> (#\1 #\2 #\3 #\4)
You can convert a char to it's ascii value with char->integer
. Eg.
(char->integer #\1) ; ==> 49
The Ascii values for #\0
... #\9
are in ascending order so you can substract to get the digit value:
(- (char->integer #\1) 48) ; ==> 1
Create a procedure where you combine these with map
and you are good to go.
(define (number-string->number-list str)
(map (lambda (ch)
<??>)
(string->list str)))
(number-string->number-list "1234") ; ==> (1 2 3 4)
Upvotes: 5