[plt-scheme] convert string to list

From: Richard Cleis (rcleis at mac.com)
Date: Sun Nov 19 03:27:08 EST 2006

; 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!
>
> _________________________________________________________________
> 与联机的朋友进行交流,请使用 MSN Messenger:  http:// 
> messenger.msn.com/cn
> _________________________________________________
>  For list-related administrative tasks:
>  http://list.cs.brown.edu/mailman/listinfo/plt-scheme



Posted on the users mailing list.