17.8.11

fireside Clojure syntax

Everyone knows that if you want to play around with a LISP, you have to actually open up a terminal and TYPE STUFF. Today, you will learn something really important about how Clojure thinks, from a common, stupid mistake.

Open up repl, and type

(interpose "|" ["A" "B"] )

This should work fine - yielding a bar separated list.

Now, add an extra set of parenthesis --- and things get really ugly.

?>((interpose "|" ["A" "B"] ))
java.lang.ClassCastException: clojure.lang.LazySeq cannot be cast to clojure.lang.IFn (NO_SOURCE_FILE:0)

What happened ? Silly rabbit : don't you know that Clojure is LISP and it is FUNCTIONAL ? In other words.... it saw your parentheses - which would normally [at the repl] comprise a function definition - [well, really, that is if unqouted - but thats for another day].

This seems trivial , like one of those errors you can glance over. Its not. You have to know how important parenthesis are in Clojure, or you'll never "get it".....

1 comment: