Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between read-string and load-string in Clojure

Tags:

clojure

I have some code which works after replacing read-string with load-string. It is good that the code works, but I would like to know why. What is the difference between the two clojure functions?

like image 513
yazz.com Avatar asked Dec 29 '10 13:12

yazz.com


1 Answers

Use load-string to sequentially read and evaluate the set of forms contained in the string

Use read-string to read one object from the string s

(both quoted from Clojure API)

Load-string will evaluate your string as a Clojure expression, and read-string takes the string and returns it as the found data structure, in which case might be an expression.

Protip: (load-string "(+ 1 2)") and (eval (read-string "(+ 1 2)")) will give you the same result, which is 3

like image 122
rownage Avatar answered Sep 21 '22 17:09

rownage