2

I am a beginner in Clojure, and I have a simple (stupid) question. I am trying to to read 4 user input , and then storing those inputs into a List

this is my code:

(def in1 (read-line)) (def in2 (read-line)) (def in3 (read-line)) (def in4 (read-line))

(def mylist '(in1 in2 in3 in4))

However, when i print the list, it gives me "in1 in2 in3 in4". How do i get it to put the value of the variables in1 in2 in3 and in4 into the List?

Thank you

1
  • 1
    I'd like to note that you're kind of overusing Lists. Make use of Clojure's vectors as well, they have some very high strong points over lists! Commented Jul 14, 2009 at 15:56

3 Answers 3

4

The single-quote in Clojure (and most Lisps) tells the system to not evaluate the expression. Thus

'(in1 in2 in3 in4) 

is the same as

(quote (in1 in2 in3 in4)

They both end up with, as you have seen, a list of symbols.

If instead you want a list of the values represented by those symbols, you can use the list form. This evaluates all of its arguments and returns a list of the results. It would look something like this:

(def mylist (list in1 in2 in3 in4))
Sign up to request clarification or add additional context in comments.

Comments

2
(def mylist (list in1 in2 in3 in4))

Comments

0

Using list is, as suggested, the thing you are looking for. If you wanted to mix evaluated and unevaluated symbols you can use syntax quoting and unquoting. For your question this is another way just in case someone is looking for this. (Notice the back-quote instead of the single-quote)

`(~in1 ~in2 ~in3 ~in4)

Comments

Your Answer

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