Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert string to code in Scheme

Tags:

lisp

scheme

How do I convert a string into the corresponding code in PLT Scheme (which does not contain the string->input-port method)? For example, I want to convert this string:

"(1 (0) 1 (0) 0)"

into this list:

'(1 (0) 1 (0) 0)

Is it possible to do this without opening a file?

like image 287
Paul Reiners Avatar asked Nov 25 '08 21:11

Paul Reiners


2 Answers

Scheme has procedure read for reading s-expressions from input port and you can convert a string to input stream with string->input-port. So, you can read a Scheme object from a string with

(read (string->input-port "(1 (0) 1 (0) 0)"))

I don't have Scheme installed, so I only read it from reference and didn't actually test it.

like image 176
Juha Autero Avatar answered Oct 13 '22 19:10

Juha Autero


From PLT Scheme manual:

(open-input-string string [name-v]) creates an input port that reads bytes from the UTF-8 encoding (see section 1.2.3) of string. The optional name-v argument is used as the name for the returned port; the default is 'string.

like image 27
Anton Nazarov Avatar answered Oct 13 '22 18:10

Anton Nazarov