ThinkGeek - Cool Stuff for Geeks and Technophiles

Over the years, I had heard much about Lisp. Articles I had read, or colleagues who knew it, would espouse the benefits of learning it or using it. Promised results ranged from making you a better imperative programmer to the highest intellectual enlightenment that could be achieved. So I dove into Lisp. Except it wasn’t diving. Well it was like diving if the body of water you are diving into is covered with 18 inches of ice, and you need to use a sledge hammer to make a hole to dive into. And then your mind and body react to diving into close to freezing water. If you come from a strictly imperative programming background, learning Lisp and/or Functional Programming can be a battle. I’m pretty sure it’s easier to learn Lisp and FP if you don’t know anything about programming.

So I started reading bits and pieces about Lisp, but without actually coding in it, I really didn’t get it. I needed to code. But what were the options? Islands. Islands with libraries that promised this and that but still little islands. Then I met Rich Hickey and his language Clojure. Clojure is a much improved dialect of Lisp that compiles down to JVM or CLR byte code. Continents. Continents with lots of libraries and documentation. I could now easily connect to SQL Server and do some real world stuff. Yes, Clojure is a dynamic language, and I’ve mentioned I prefer static languages, but Clojure is filled with awesome.

Although Clojure along with other Lisps are functional programming languages, Lisps have a very unique property that other common FP languages don’t have: Homoiconicity. Thats fancy talk for saying the code and data have the same shape. Data structures and code are represented the same way: s-expressions. This is what drives the powerful Lisp macro system. The macro system is what makes the language changeable. If there is a feature you want that’s not in the language, you can add it. This point was really driven home to me when I learned about the F# pipeline operator: |> . I thought, see this is a nice feature that Clojure doesn’t have. Until I realized that someone had built a pipeline macro for Clojure. This is from core.clj:

(defmacro ->
  "Threads the expr through the forms. Inserts x as the
  second item in the first form, making a list of it if it is not a
  list already. If there are more forms, inserts the first form as the
  second item in second form, etc."
  ([x form] (if (seq? form)
              `(~(first form) ~x ~@(next form))
              (list form x)))
  ([x form & more] `(-> (-> ~x ~form) ~@more)))

If F# didn’t have |> there would be nothing you could do [Update: That is not true. As Brian pointed out in the comments, F# allows you to declare infix operators.]. Don’t get me wrong, I’m a HUGE fan of F#, it just doesn’t have a macro system. But it is statically typed :)

2 Responses to “Clojure and Lisp”

  1. Brian says:

    Just a note: in F#, |> is just another infix operator. You can define it yourself: “let (|>) x f = f x”. It just happens to be defined in the core library because it is handy.

    Macros can indeed be handy, but you don’t need macros to create user-defined operators in F#.

  2. Mark says:

    Good point. Updated post.