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
我是Clojure的初学者,我有一个简单(愚蠢)的问题。我试图读取4个用户输入,然后将这些输入存储到List中
this is my code:
这是我的代码:
(def in1 (read-line)) (def in2 (read-line)) (def in3 (read-line)) (def in4 (read-line))
(def in1(读取线))(def in2(读取线))(def in3(读取线))(def in4(读取线))
(def mylist '(in1 in2 in3 in4))
(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?
但是,当我打印列表时,它给了我“in1 in2 in3 in4”。我怎样才能将变量in1 in2 in3和in4中的值放入List中?
Thank you
3 个解决方案
#1
(def mylist (list in1 in2 in3 in4))
#2
The single-quote in Clojure (and most Lisps) tells the system to not evaluate the expression. Thus
Clojure(和大多数Lisps)中的单引号告诉系统不要评估表达式。从而
'(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))
#3
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)
#1
(def mylist (list in1 in2 in3 in4))
#2
The single-quote in Clojure (and most Lisps) tells the system to not evaluate the expression. Thus
Clojure(和大多数Lisps)中的单引号告诉系统不要评估表达式。从而
'(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))
#3
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)