Page 1 of 1

Thinking in Clojure vs Common Lisp

Posted: Wed Apr 29, 2009 4:45 pm
by billy
Hi, I'm relatively new to the Lisp world aside from hacking on my .emacs.

I'm reading through my beta copy of Programming Clojure, and there seems to be more of focus on creating lazy sequences as opposed to recursive functions (mostly because of the lack of tail call optimization in the JVM).

I was wondering if someone could compare that to thinking in Common Lisp. Does CL have lazy sequences? If so, is this the preferred way to structure your program?

I'm know I want to use Lisp for a web app project I'm working on, I'm just trying to decide between the two. I kinda assumed that there was just some syntactical differences between the Lisp dialects, now I'm seeing that the differences may extend much further.

Thanks.

Re: Thinking in Clojure vs Common Lisp

Posted: Thu Apr 30, 2009 3:02 am
by ramarren
The usual way to structure a program in Common Lisp is to use neither recursion nor lazy sequences, but stateful iteration. There is a number of libraries giving lazy sequences or similar, for example series, but they are rarely used. Recursion is relatively more common, but CL standard doesn't guarantee tail call optimization.

Re: Thinking in Clojure vs Common Lisp

Posted: Thu Apr 30, 2009 1:32 pm
by Paul Donnelly
Thinking in Common Lisp is deciding what you want to do, then doing it. If lazy sequences are that thing, then go right ahead. This isn't Python, where there's one true way to write your code. Most Lispers would turn to something else first, but having the flexibility to do as you think best is the whole point of writing in Lisp.

Re: Thinking in Clojure vs Common Lisp

Posted: Sun May 03, 2009 3:44 pm
by Unne
There's nothing wrong with using recursion in Clojure. Use loop + recur, it works fine 99% of the time. I very rarely find myself using loop + recur though; they are more low-level than you usually need. map, reduce, filter, for (list comprehensions) and friends are better.

Stateful iteration in Clojure doesn't make sense because the data structures you're (usually) working with are immutable. You should instead be taking some structure and producing a new structure based on it (which is why map and friends fit well). The immutable data structures are the main place you have to change your thinking in my opinion; once you get used to those, everything else clicks into place.

Laziness can be safely ignored or taken for granted most of the time (but keep in mind that it's going on, so you know when to force evaluation via dorun/doall for example). The Programming Clojure book does go a lot into lazy sequences but I think they only really make sense for certain kinds of problems, e.g. dealing with infinite lists and such.

Having written a web app in Clojure and Common Lisp, I personally prefer Clojure, but both work fine. Compojure is fun.