[plt-scheme] convert string to list

From: Eli Barzilay (eli at barzilay.org)
Date: Sun Nov 19 05:42:57 EST 2006

Alternatively:

  (require (lib "string.ss"))
  (read-from-string-all "55 34 rr =")

Which is doing something very similar.

On Nov 19, Richard Cleis wrote:
> ; You might want to use the function 'read' to extract each value.
> ; To see how that can be done, first try this:
> 
> (define the-string "55 34 rr =")
> (define port (open-input-string the-string))
> (read port)
> (read port)
> (read port)
> (read port)
> (read port)
> (close-input-port port)
> 
> ; result:
> ; 55
> ; 34
> ; rr
> ; =
> ; #<eof>
> 
> ; A general function can be written in many ways.  You say that
> ; you are learning Scheme, so the following example uses very few
> ; Scheme concepts.  If you learn more about Scheme, you can
> ; make it nicer:
> 
> (define (list-string-elements port val)
>    (cond ((equal? val eof) ; string port is exhausted...
>           null)            ; ... so terminate list with null
>          (else (cons val   ; construct val on beginning of list
>                      (list-string-elements port (read port))))))
> 
> (define port (open-input-string the-string)) ;open the string port
> (list-string-elements port (read port))      ; provide first value
> 
> ; result:
> ; (55 34 rr =)
> 
> 
> rac
> 
> On Nov 18, 2006, at 12:12 PM, xue haifeng wrote:
> 
> > I am new to using Scheme. I am trying to convert a string to a  
> > list. It needs the list to be like '(55 34 rr = ) and not '(#\5 #\5  
> > #\space #\3 #\4 #\space #\r #\r #\space #\= ). Is there a simple  
> > way to get the list '(55 34 rr =) from the string "55 34 rr =".
> > Thanks for any help you have!

-- 
          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                  http://www.barzilay.org/                 Maze is Life!


Posted on the users mailing list.