清单声明中的Clojure清单 [英] Clojure list inside list declaration
问题描述
我试图在Clojure的list内声明一个列表.
I was trying to declare a list inside list in Clojure.
Expected behavior: `(`()) => (())
Actual behavior: `(`()) => ((clojure.core/list))
该输出是什么意思?
此外,我想了解以下行为如何保持一致.
Also, I would like to understand how the behavior below is consistent.
`() => ()
`("hi") => ("hi")
`(`()) => ((clojure.core/list))
与我的问题无关,这是一个实际上返回(())
的代码段:
Unrelated to my question, here's a code snippet which actually returns (())
:
(conj `() `())
推荐答案
基本上,不要嵌套引号.我将在此处使用基本的 quote
特殊形式,但适用相同的概念以及更复杂的语法引用.
Basically, don't nest quotes. I'm going to use the basic quote
special form here, but the same concepts apply to the more complex syntax quote as well.
当您写这篇文章时:
'()
;;=> ()
与编写此代码完全相同:
(quote ())
;;=> ()
因此,当您编写此代码时:
So when you write this:
'('())
;;=> ((quote ()))
与编写此代码相同:
(quote ((quote ())))
;;=> ((quote ()))
您可以做的一件事就是引用最外面的列表:
One thing you can do is just quote the outermost list:
'(())
;;=> (())
或者您可以 list
函数,这是一种更为通用的解决方案:
Or you can the list
function, which is a far more general solution:
(list)
;;=> ()
(list "hi")
;;=> ("hi")
(list (list))
;;=> (())
(list 1 (+ 1 1) 3)
;;=> (1 2 3)
这篇关于清单声明中的Clojure清单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!