David Merinos
David Merinos

Reputation: 1297

String containing a list into an actual list

I was just wondering if there's a way to make: "((a b))" into '((a b)).

I used (symbol->string "((a b))") which gives me '|((a b))|

And that's not exactly what I need. I need a list.

Upvotes: 2

Views: 82

Answers (2)

Óscar López
Óscar López

Reputation: 236150

Simply do this in Racket:

(call-with-input-string "((a b))" read)
=> '((a b))

The advantage of using call-with-input-string is that the string port is automatically closed, as has been mentioned before.

Upvotes: 6

colevk
colevk

Reputation: 688

You can use read and open-input-string together to parse strings into lists.

> (define (string->list str) (read (open-input-string str)))
> (string->list "((a b))")
'((a b))

This converts the string first into an input-port (a data stream) and then from an input-port into a list. See the documentation for open-input-string and read.

Upvotes: 3

Related Questions