1

I came across this macro definition for unless from the brave and true book

(defmacro unless
  "Inverted 'if'"
  [test & branches]
  (conj (reverse branches) test 'if))

I believe the rest param is a sequence, and the conj returns a sequence, so this entire macro returns a sequence. I thought you needed to return a list for the return to be evaluated properly

On further investigation, why does (eval (sequence [+ 1 4 4])) do the same thing as (eval (list 1 4 4)). Where does it say that sequences are evaluated like lists? I don't see that in the docs. –

1
  • 1
    This is a fault in the documentation of Clojure evaluation. The critical sentence is "Non-empty Lists are considered calls to either special forms, macros, or functions.", wherein list ought to be replaced by sequence. Commented Aug 10, 2019 at 22:50

1 Answer 1

2

You have just proven that a list, a seq, and a sequence are all treated as function call sites by the Clojure compiler. That is your answer.

=> (def xxx [+ 2 3])  ; a function and 2 integers in a vector

=> (eval xxx) ; a vector doesn't work
[#object[clojure.core$_PLUS_ 0x375dfecb "clojure.core$_PLUS_@375dfecb"] 2 3]

=> (eval (seq xxx))  ; a seq works
5

=> (eval (sequence xxx))  ; a sequence works
5

=> (eval (apply list xxx))  ; converts xxx to a list  (+ 2 3)  which works
5

When the Clojure docs are ambiguous or are missing some detail, a small experiment like the above will answer your question.

If the answer applies to a specific area of the Clojure website, function docstring, or clojuredocs.org or clojure-doc.org, you may consider submitting a pull-request to update & improve the docs.

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

4 Comments

That implies there are no bugs in the implementation, just like perl5. Telltale sign of a bad language.
@Sylwester It seems to be a well designed language if you ask me.
@Thomas A well designed language would have a well defined specification one can reimplement the language from that would be the source of truth whenever the two has different result on the same code. In Perl5 it states that if you find there is discrepancies between the language and documentation, clearly the documentation is wrong. Perl6 does it the other way, mostly because there are no feature complete perl6 implementations yet. The Clojure documentation doesn't seem complete enough to be the source of truth.
It is true that Clojure does not have anything analogous to the Java Language Specification; however, that is a very expensive & time-consuming item to create (772 dense pages on the Oracle website). The Clojure docs are analogous to JavaDocs; they are meant for everyday usage & reference. While there will always be shortcomings and gaps, ranting about imperfection benefits no one. As with any open-source project, contributions to improve the project and its documentation are always welcome.

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.