Devashish Dixit
Devashish Dixit

Reputation: 1193

Scheme : Parsing a string

I want to convert a string "(1 2 3 4)" to a list (1 2 3 4).
How can this be done using Scheme?

Upvotes: 3

Views: 1499

Answers (2)

soegaard
soegaard

Reputation: 31147

Check out the Guide in Racket:

http://docs.racket-lang.org/guide/read-write.html?q=read

Upvotes: 0

user725091
user725091

Reputation:

You could use the built-in read function by turning the string into an "input port" (an abstraction of a file opened for reading):

(read (open-input-string "(1 2 3 4)")) ;; evaluates to (1 2 3 4)

That works in both Guile and Racket. Depending on your Scheme implementation, you might also need to import the SRFI-6 module.

Upvotes: 5

Related Questions