Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert string to parenthesized S-expression in Racket

How can I convert a string like "(+ ( - 5 2) 8)" to an s-expression (+ (- 5 2) 8) that can be evaluated in the Racket prompt? I tried string->symbol but it returns '|(+ ( - 5 2) 8)| which is not what I want.

like image 735
user1472346 Avatar asked Dec 03 '25 23:12

user1472346


1 Answers

The read function in racket (or any other lisp) does just this. Except read will read from an input port rather than a string. You can use open-input-string function for that.

(read (open-input-string "(+ (- 5 2))"))
like image 138
merlyn Avatar answered Dec 07 '25 05:12

merlyn