0

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.

2 Answers 2

5

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\"")

Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.