119 points · by muriithi · 11y ago · 120 comments
Either way, if you get the terminal condition of a recursive function wrong, you will blow the stack. It's true Clojure isn't built to do TCO, but that's a non-sequitur. The idea that you don't typically use recursion in Clojure is ridiculous - for is a macro, it's syntactic sugar over Clojure's recursion primitives, loop and recur. Even if you didn't want to use that, change the nil? check to empty? and it works. I'm not going to claim that Clojure's list/sequence semantics are necessarily the cleanest (the empty _sequence_ in Clojure is nil, an empty list/vector/set etc is not), but they're not hard to learn if you make any effort at all. Again, your existing Lisp experience may or may not help you understand Clojure's data structures, but I am surprised that this is a surprise to anybody.
Not to say recursion is not important. Without it lambda calculus is not Turing complete. But Clojure does provide explicit TCO. Saying this is a big difference is excessive IMHO.
Not in Common Lisp. The last time I tried to show someone how well recursion worked in a typical Common Lisp implementation, I crashed Firefox trying to paste the result of a naive factorial function into a text buffer. Stack depth limits in other languages are often arbitrary.
> First, this code assumes that (if (null list1)) in Common Lisp will be equivalent to (if (nil? list1)) in Clojure, but Clojure doesn't consider an empty list to have a nil value.
That sentence makes it very clear the writer either hasn't been arsed to read through even one Clojure tutorial or is being intentionally dense. If they have an educated opinion, great, but I have no patience for this level of ignorance, especially if it's feigned ignorance.
If one looks at music software in Common Lisp, the code is on a higher level than building lists like that.
> Secondly, this code tries to handle lists in the classic Lisp way, with recursion, and that's not what you typically do in Clojure.
Neither is it done in Common Lisp.
> Those 7 lines of Common Lisp compress to 2 lines of Clojure
The actual difference is that Common Lisp uses LOOP and not FOR, and that LOOP needs two nested LOOP forms:
CL-USER 11 > (defun build (list1 list2)
(loop for e1 in list1 append
(loop for e2 in list2 collect (list e1 e2))))
BUILD
CL-USER 12 > (build '(1 2) '(a b))
((1 A) (1 B) (2 A) (2 B))
Two iteration forms in a LOOP don't nest, but provide iteration bindings similar to LET* and LET: (loop for i in '(1 2 3) for j = (expt i 2) collect (list i j))
and (loop for i in '(1 2 3) and j in '(1 4 9) collect (list i j))
Common Lisp's ITERATE macro also needs nested forms, but slightly improved over LOOP: ITER-USER 26 > (iterate outer (for i in '(1 2))
(iterate (for j in '(a b))
(in outer (collect (list i j)))))
((1 A) (1 B) (2 A) (2 B))
The author of Clojure knows these differences very well, since he was a heavy Common Lisp user for a few years.> But, at the same time, if you're looking to translate stuff from other Lisps into Clojure, it's not going to be just copying and pasting. Beyond inconsequential, dialect-level differences like defn vs. defun, there are deeper differences which steepen the learning curve a little.
That's true. Clojure is not a Lisp, but partially derived from it, with many other influences from Haskell and other languages. It's mostly incompatible to Lisp: software can't be shared or copied, it needs to be complete rewritten. Thus basic Lisp literature is only of use when it's about features which got copied. For example the book 'On Lisp' might help to understand macros in Lisp and Clojure, whereas books like 'Practical Common Lisp' or Norvig's PAIP aren't very useful for Clojure programmers.
Clojure is a Lisp. It may not be Lisp, but it's a Lisp. Just like any nix OS is not UNIX but is a Unix OS.
Clojure is most definitely a dialect of Lisp. From clojure.org:
> Clojure is a dialect of Lisp, and shares with Lisp the code-as-data philosophy and a powerful macro system.
The intent of the design of Clojure was clearly to be a dialect of Lisp, hence the above quote from Rich Hickey proudly displayed on their site.
LISP appeared in the late 50's/early 60's. Common Lisp only appeared in the early-to-mid 80's. There were many other lisps in between and many since. Common Lisp is only one of many dialects of Lisp. Clojure is another.
(Not saying that this is what lispm did in this case, (s)he seems to know a lot about lisp; rather this is a common confusion I've seen)
Take code from any Lisp book from the past 50 years and type it into Clojure. It won't work. Even really basic Lisp code won't work.
> There were many other lisps in between and many since.
They all share a core language.
Simple REPL in Emacs Lisp:
ELISP> (defun read-eval-print-loop ()
(do* ((input (read) (read))
(value (eval input) (eval input)))
((equal input '(quote quit))
'done)
(print 'input>)
(princ input)
(print 'value>)
(princ input)
(print value)))
read-eval-print-loop
ELISP> (read-eval-print-loop)
input>
(mapcar (quote +) (quote (1 2 3)))
value>
(mapcar (quote +) (quote (1 2 3)))
(1 2 3)
done
Now OpenLisp, an implementation of ISLisp.Code unchanged.
; OpenLisp v10.1.0 (Build: 5932) by C. Jullien [Aug 30 2014 - 07:38:27]
;; WARNING: \"startup.lsp\" file not loaded.
? (defun read-eval-print-loop ()
1> (do* ((input (read) (read))
3> (value (eval input) (eval input)))
2> ((equal input '(quote quit))
3> 'done)
2> (print 'input>)
2> (princ input)
2> (print 'value>)
2> (princ input)
2> (print value)))
;; elapsed time = 0.000s, (0 gc).
= read-eval-print-loop
? (read-eval-print-loop)
? (mapcar #'1+ '(1 2 3))
input>
(mapcar #'1+ '(1 2 3))value>
(mapcar #'1+ '(1 2 3))(2 3 4)
The output lacks correct formatting, but generally it works.The same code unchanged in Common Lisp:
CL-USER> (defun read-eval-print-loop ()
(do* ((input (read) (read))
(value (eval input) (eval input)))
((equal input '(quote quit))
'done)
(print 'input>)
(princ input)
(print 'value>)
(princ input)
(print value)))
READ-EVAL-PRINT-LOOP
CL-USER> (read-eval-print-loop)
(mapcar '+ '(1 2 3))
INPUT> (MAPCAR (QUOTE +) (QUOTE (1 2 3)))
VALUE> (MAPCAR (QUOTE +) (QUOTE (1 2 3)))
(1 2 3)
'quit
DONE
Every language I would name Lisp runs this code mostly unchanged.If it does not, it is not Lisp. Then it would be Scheme, Logo, Dylan, Clojure or some other language.
That code from other lisp implementations needs rewritten to be ported, is really not that much different from how many other lisp implementations were to each other.
CL and Scheme did a lot to unify things, such that any scheme should run another scheme's code. Same for any CL implementation. But, for example, running emacs lisp in any other lisp just isn't going to work.
CL-USER> (defun fact (n) (do* ((i n (- i 1)) (j i (* j i))) ((= i 1) j)))
FACT
CL-USER> (fact 15)
1307674368000
Unchanged code in Elisp... ELISP> (defun fact (n) (do* ((i n (- i 1)) (j i (* j i))) ((= i 1) j)))
fact
ELISP> (fact 15)
1307674368000 (#o23016735654000, #x13077775800)
Elisp originally was designed as a Maclisp subset. Common Lisp was designed as a Maclisp successor.That does not say anything about whether or not a language is an assembly. Just as this says nothing about whether a language is a lisp.
But there are a lot of languages in the Lisp family that don't share the syntax (except from the basics, maybe), nor the standard libraries. See e.g. http://blog.fogus.me/2011/05/03/the-german-school-of-lisp-2/
In any discussion involving Common Lisp, there will be the opinion that "Clojure is not a Lisp" without (in my view) providing sufficient context for this clearly confusing statement.
(FWIW, I call Clojure a Lisp. It is clearly in the Lisp tradition, not to mention more technical things like: code-is-data, serious REPL, accurate numbers like 10.00000000000000001M. I don't think "Lisp" should be reserved for mainly incremental improvements on an old movement.)
Like Prolog or Julia.
I keep seeing people make this statement, without any explanation of why. To me, hardly a heavy user of other Lisps, but with enough time spent in Common Lisp and Scheme to have an idea of what's going on, it feels pretty lispy.
Is C++ a C? Is Oberon a Pascal? Is F# a ML? Is C# a Java? Is Go a Squeak? Is Objective-C a Smalltalk? Is Javascript a Self?
Just think of it as civil law (versus common law) when interpreting it.
LOOP is something that imho very controversial. I don't think it's fair to say that its use is idiomatic.
No, not really: it's as controversial as Common Lisp's Lisp-n nature (which is to say, both of which are controversial amongst Scheme programmers writing Common Lisp). It's a perfectly useful macro. ITERATE is a bit more traditionally Lispy, and is an easy library away if one wants it.
> I don't think it's fair to say that its use is idiomatic.
Not using it is typically less clear. It's part of the standard; it's more elegant than using other parts of the standard; it's idiomatic.
Simple example from Winston&Horn's book LISP for a database query language:
(db-select screwdrivers with color eql red)I wish the author would have spent a little bit more time researching. The comparison is just plain sloppy. Why is Clojure's `recur` not mentioned, for instance? This is documentation that's not that difficult to find by googling, and is mentioned in (I believe) every Clojure book available.
1. It's a lisp, which means homoiconic, i.e. build software just like Lego (different from non-lisps)
2. Immutability and purity in the core library + sane, managed concurrency via atoms, refs, core.async, i.e. write serious multithreaded code without putting your hair on fire; helps on the front-end via ClojureScript as well (different from most other Lisps)
3. The JVM, which means very good performance in the general case + build once, run everyone + a lot of libraries
(defun build (list1 list2)
(loop for x in list1 append (loop for y in list2 collect `(,x ,y))))
or: (defun build (list1 list2)
(mapcan (lambda (x) (mapcar (lambda (y) `(,x ,y)) list2)) list1))
Still more verbose, of course, but not at all bad.The lack of TCO in JVM hosted languages is besides the point.
It is also possible for the Clojure compiler to do TCO in certain cases: Rich Hickey made the conscious decision to not do it.
If I read your statement as Rich Hickey having the opportunity but passed on it, I'd reply that he did that with recur. Unless you mean silent TCO like e.g. Scheme does.
It certainly looks more elegant to do it silently, but Clojure doesn't strive for that sort of elegance.
(defun foo () (foo)) ;<- tail call
(defun bar () (bar)) ;<- tail call
(defun baz (a b) (+ ;<- tail call
(* a 2)
(* b 3)))I was gonna post the question "Why should I learn clojure?" but that's easy to google for and get good articles.
I was then gonna post "What's a good book for learning Clojure?" but I can get that on Quora.
So my real question is: what can I read about Clojure that will get me up to speed on its unique awesomeness? I don't need a tutorial that shows me how to add two ints or invoke a function. Show me the good stuff!
The destructuring mechanisms in Clojure are very, very powerful. For some reason this author does not seem to like them, even though they reduce his 7 line CL program to a 2 line Clojure program. That's not a fluke, from what I've seen. Stuff that would require a lot of caddars and cddaars and whatnot in a normal Lisp are much more straightforward in (idiomatic) Clojure.
The interop between Clojure and Java (or ClojureScript and JavaScript) seems to be a lot cleaner and more straightforward than the sometimes-ugly FFIs you see with other languages.
It has just about all the standard Lisp goodies, too (macros, etc.)
What destructuring? I didn't see any in the example.
I'd say start here, it will get you in the mindset of Clojure.
I read a fair amount of both and the former explains the subject matter too superficially IMHO. +1 for Joy of Clojure which is a nice read in parallel.
Joy of Clojure is a good book. It goes deeper while assuming more of the reader. The technical detail is useful and interesting, but for me, the narrative seems a bit less cohesive [disclaimer: I have the first edition, not the second]. I haven't read Clojure Programming.
[1]: Edit. For example the running exercise is porting a Java build system to Clojure. That's full on software engineering, not a let's-pretend.
In any case, at Standard Chartered we didn't have tail call optimization with our Haskell dialect either. It didn't matter too much in practice, because you should be using combinators anyway. And when you are calling foldr or map, you do not care that somewhere hidden away they are implemented with a loop in C++, as long as they behave right.
map :: (a -> b) -> [a] -> [b]
map f [] = []
map f (x:xs) = f x : map f xs
But it is definitely still true that explicit recursion is discouraged as being too 'low-level' for most Haskell code and it's preferable to use higher-order functions instead.Yes, GHC can and does just use the recursive goodness, and compile it away to no-stackframe-adding jumps.
And really i think the "Classic" lisp way to loop is loop, not recursion.
Scheme forces the point and requires full TCO.
The ones that don't provide any form of TCO are old Lisp Machine implementations and ABCL on the JVM.
SBCL, OTOH, provides full TCO.
Some older overview about Common Lisp implementations and their TCO support:
Both, I think (from long ago memories). The "modern" loop macro (which is probably Turing complete like I seem to remember people saying format is :-) is I think a relatively new thing, I overhead a lot of discussion about its design in the early '80s.
Although it probably had precursors, mainline Lisp, now Common Lisp, is decidedly multi-paradigm, there were even sops thrown to FORTRAN programmers as I recall, probably back from when there were only a very few computer languages in existence (heck, LISP's first implementation, on a vacuum tube computer, was as FORTRAN subroutines). So overt things like loop are in theory idiomatic as well as recursion.
The --full-tail-calls flag is not on by default, partly
because it is noticably slower (though I have not measured
how much), and partly I think it is more useful for Kawa to
be compilatible with standard Java calling conventions and
tools.
Well, for some definition of just fine. Well implemented TCO is a performance boost, not hit on most platforms. The lack of TCO built into the JVM means that JVM languages like Scala and Kawa generally have to roll their own on top of the JVM, resulting in a performance hit.Kawa now does general tail-call elimination, but only if you use the flag --full-tail-calls. (Currently, the eval function itself is not fully tail-recursive, in violation of R5RS.) The --full-tail-calls flag is not on by default, partly because it is noticably slower (though I have not measured how much), and partly I think it is more useful for Kawa to be compilatible with standard Java calling conventions and tools. Code compiled with --full-tail-calls can call code compiled without it and vice versa.
(defun build (list-1 list-2)
(loop
:for elem-1 :in list-1
:for elem-2 :in list-2
:collect (list elem-1 elem-2)))
A much bigger difference imho between Clojure and Common Lisp is that the former is built on abstractions, conj, while the latter is built on concrete cons cells. There are more other significant differences but I don't want to flame/argue.Afaik it is possible in racket. Read the note at the end here: http://matt.might.net/articles/lexers-in-racket/
Edit: Jesus Christ, Clojure doesn't even have conses. Why the hell do people call this a "Lisp"?
> (= () nil)
false
> (('a . 'b) . ('a . 'b))
java.lang.RuntimeException: Unable to resolve symbol: . in this context
> (cons 'a 'b)
java.lang.IllegalArgumentException: Don't know how to create ISeq from: clojure.lang.Symbol > (cons 'a ['b])
(a b)
> (doc cons)
-------------------------
clojure.core/cons
([x seq])
Returns a new seq where x is the first element and seq is the rest.But it's still a Lisp, I went straight from old half-remembered mainline LISP and Scheme to Clojure in a recent small web server project without difficultly. The dynamic style of development is the same as is the typing, if you're not using lists, then the OPs problems don't come up (idiomatic Clojure web programming uses maps, key value pairs), the syntax is still s-expressions, albeit polluted by arrays denoted with square brackets where it makes sense.
And I believe the article is wrong in one sense, the JVM treats non-tail recursion like other languages, growing the stack. It's tail call optimization (TCO) that's the issue: mandatory in Scheme, don't know about its prevalence in Common Lisp implementations, and it's awkward in Clojure but wasn't much of a jump from SICP for the typical cases. And I think it might be a good idea to require signaling when you intend to tail recurse, it's easier and much quicker to find in compilation than when you blow the stack running it.
While I very much like the first class syntax for arrays, maps, and sets, there's a tremendous advantage to having one main or even exclusive built in composite data type, e.g. I hear that a strength of Lua, which I gather has been very successful. LISP's DNA, as well as Scheme's I'm pretty sure, was established in the days when that was lists.
So clojure tried principled consistency, I like the logic of it, but not the result. Erik Meijer and Brett Victor claim it's a good thing to do. Find a concept and go all the way down with it.
[1] it would be fun to be denotational even for these. (arithmetic-sum (number two one three) (number six six six)), yes it would be.
Don't lisp advocates usually say it has no syntax.
would you give an example of this? Just curious as someone who has spent more time in clojure than in lisp.