0

I was trying to declare a list inside list in Clojure.

Expected behavior: `(`()) => (())
Actual behavior:   `(`()) => ((clojure.core/list))

What does that output mean?

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 `() `())

1 Answer 1

2

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.

When you write this:

'()
;;=> ()

That's exactly the same as writing this:

(quote ())
;;=> ()

So when you write this:

'('())
;;=> ((quote ()))

That's the same as writing this:

(quote ((quote ())))
;;=> ((quote ()))

One thing you can do is just quote the outermost 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)
Sign up to request clarification or add additional context in comments.

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.