Reputation: 3933
I am fairly new to functional programming. I am learning clojure. I was trying out a few commands. Some of them work fine. I got this weird one below:
(read-string "1 2 3")
;;this ouputs
1
(read-string "[1 2 3]")
;;this ouputs
[1 2 3]
I am wondering why in the first one it doesn't output the complete string. but does output it in the second one. 1 2 3
Any reason for this?
In case you are wondering, i was doing this on command line. It shouldn't really matter.
Upvotes: 0
Views: 51
Reputation: 1322
The meaning of "string" in the name read-string
is meant to indicate that its argument is a string, not its return value. The point of it is to parse a single piece of clojure syntax from the given string, which it is doing in both of your examples. The string "1 2 3"
contains three forms instead of just one. read-string
always parses and returns a single form, so returning 1
is the expected behavior here.
Upvotes: 2
Reputation: 9266
None of your examples output a string.
As the docstring says: "Reads one object from the string s."
In the first case the object is 1
, in the second case the object is [1 2 3]
.
To output a string the object needs to be a string, e. g. (read-string "\"1 2 3\"")
Upvotes: 5