Translating Lists From Common Lisp to Clojure
While lists are common in Clojure, they aren't as central as they are in Common Lisp. Translating list-oriented concepts from Common Lisp to Clojure needs some finesse.
Join the DZone community and get the full member experience.Join For Free
the list is a very central data structure in common lisp, especially when looking at educational content (such as classic books). in clojure, lists exist and are used quite a bit, but their place is less central, considering their inherent inefficiency compared to other data structures is acknowledged. this often leaves us with a need to translate a lot of list-oriented idioms from common lisp to equivalent clojure code.
when working with lists, common lisp often uses the predicates consp and listp; it turns out that neither of these has a direct equivalent in clojure, and we must think carefully when translating code using consp or listp.
throughout this post, i assume cons cells are only used to construct proper lists, such as (cons 'a (cons 'b nil)). other uses of cons in common lisp are fairly rare; this fact is acknowledged by the language specification. for example, listp returns true on a non-list cons cell, since it's assumed that cons cells are used mostly for lists:
> (cons (cons 1 2) 3) ((1 . 2) . 3) > (listp (cons (cons 1 2) 3)) t
the consp predicate simply checks if its argument is a cons cell:
> (consp (cons (cons 1 2) 3)) t > (consp (cons 1 (cons 2 nil))) ;; list using cons t > (consp '(1 2)) ;; nicer way make the same list t
it will reject anything that's not a cons cell, most notably symbols and numbers:
> (consp 1) nil > (consp 'foo) nil
an unfortunate side effect is that consp also rejects nil; but nil is used to mean an "empty list" in common lisp (it's equivalent to '()), so consp is not a convenient function to use to check whether something "is a list". that's what listp is for. listp is simply an or between the consp and null predicates:
> (consp nil) nil > (listp nil) t > (consp '()) nil > (listp '()) t
as mentioned above, listp simplistically accepts non-list cons cells as lists as well, but that's rarely a problem in practice.
now, if you're reading some book or article that uses common lisp as its demonstration language, there's a good chance calls to consp and listp are nonchalantly scattered all over the code; the difference between these two, as we've just seen, is quite subtle, and authors use one or the other based on the nature of the problem faced. for example, if you see a consp, it's most likely there because the author wants to explicitly reject empty lists.
in clojure, our first attempt would be to use seq. it returns a truthy value for sequences, and can be used within conditions:
cl-in-clj.core=> (if (seq (cons 1 (cons 2 nil))) 'yes 'no) yes cl-in-clj.core=> (if (seq '(1 2)) 'yes 'no) yes
this appears to be a reasonable replacement for consp, until we try it with a symbol:
cl-in-clj.core=> (if (seq 'foo) 'yes 'no) illegalargumentexception don't know how to create iseq from: [...]
oops. that's not good. we need a better way to detect lists that won't blow up on us. an alternative is
cl-in-clj.core=> (list? '(1 2)) true cl-in-clj.core=> (list? 'foo) false cl-in-clj.core=> (list? '()) true
but wait, this returns true for '(); so it's more like an equivalent for listp than for consp. for consp, we'd need to make sure we return false for an empty list:
cl-in-clj.core=> (defn my-consp [obj] (and (list? obj) (not (empty? obj)))) #'cl-in-clj.core/my-consp cl-in-clj.core=> (my-consp '(1 2)) true cl-in-clj.core=> (my-consp 'foo) false cl-in-clj.core=> (my-consp '()) false
note that i'm using (not (empty? ...)) rather than the more idiomatic (seq ...) ; this is on purpose. if the object is a sequence, seq will return it and that will be the value of the and. sometimes, i really only want a boolean, so (not (empty? ...)) is more accurate.
there's another issue here, having to do with nil. in common lisp, nil is just the empty list. in clojure, they're different. the list? defined above will return false for nil, unlike its common lisp counterpart. the real solution to this in clojure is to avoid using nil to mean the empty list; instead just use the empty list '(). this means we have to be careful when calling first on an empty list, since it returns nil.
while on the topic of nil, common lisp's null is true on the empty list while clojure's (nil? '()) is false. to approximate the behavior of null, we could do:
cl-in-clj.core=> (defn my-null [obj] (or (nil? obj) (empty? obj))) #'cl-in-clj.core/my-null cl-in-clj.core=> (my-null nil) true cl-in-clj.core=> (my-null '()) true
to conclude, if you need common lisp's listp, use clojure's list? but be aware of nils. it's best to avoid nils in clojure list processing, but this isn't always trivial since (first '()) returns nil.
if you need consp, you'll have to define something like my-consp , unless you are ok with an exception being thrown on symbols and numbers, in which case seq should do.
Published at DZone with permission of Eli Bendersky. See the original article here.
Opinions expressed by DZone contributors are their own.