Clojure and the CLR

From the Clojure home page:

Clojure is a dialect of Lisp, and shares with Lisp the code-as-data philosophy and a powerful macro system. Clojure is predominantly a functional programming language, and features a rich set of immutable, persistent data structures. When mutable state is needed, Clojure offers a software transactional memory system and reactive Agent system that ensure clean, correct, multithreaded designs.

I’ve had an interest in Clojure ever since I saw Craig Andera do a talk on the topic in D.C. (Craig also has a set of Clojure videos @ Pluralsight). It was just an intellectual fascination until clojure-clr appeared and things took a turn towards the possibly practical.

Why Am I Interested in Clojure?

As a dialect of Lisp, Clojure embodies the spirit of the greatest single programming language ever designed, including the parentheses, prefix notation, and a simple, clutter-free syntax.

Clojure C#
; Comparing 5 and 3+2 returns 0
(println (compare 5 (+ 3 2)))
// Comparing 5 and 3+2 returns 0
Console.WriteLine(5.CompareTo(2 + 3));

Functions are first class citizens in Clojure, of course, so you can pass functions as parameters and return them as the result of other functions. There are closures, currying, and recursive looping. Fans of LINQ and functional programming with Func<> and Action<> on the CLR should know that you won’t be missing anything with Clojure, in fact, there is a whole new world to explore.

Clojure C#
(take 2 (reverse 
  (filter (fn [s] (= \L (first s))) 
    ["Minnesota", "Detroit", "London", "Las Vegas"] )))
var places = new string[] 
    { "Minnesota", "Detroit", 
       "London", "Las Vegas" }
    .Where(s => s.First() == 'L')
    .Reverse()
    .Take(2);

Also, like any LISP, the compiler offers extensibility through macros. Program code is data you can evaluate or manipulate. However, the hallmark of Clojure is the inherent support for safe and simple concurrency. I plan on using Clojure in some data analysis work, and we'll explore interop and threading in a future post.

Print | posted @ Tuesday, August 16, 2011 9:12 AM

Comments on this entry:

Gravatar # re: Clojure and the CLR
by Aaron Fischer at 8/17/2011 11:32 AM

How would this compare to F#?
  
Gravatar # re: Clojure and the CLR
by scott at 8/17/2011 2:57 PM

@Aaron: lots of similarities, I think. Both are functional, both have immutable data structures, both offer easy async and concurrent operations.

I think the primary differences (other than syntax) would be that Clojure is dynamic, offers transactional memory, and runs on both the CLR and the JVM.
  
Gravatar # re: Clojure and the CLR
by Neyah at 8/17/2011 3:19 PM

Can you check your blog css? The link hover color of white combined with the faint yellow background makes the link text impossible to read when hovering. At least for me.
  
Gravatar # re: Clojure and the CLR
by Doeke at 8/18/2011 8:10 AM

Why does a language, that uses polish notation (+ 3 2 instead of 3+2) still need an overuse of brackets?
  
Gravatar # re: Clojure and the CLR
by scott at 8/18/2011 12:20 PM

@Doeke: Will fix this weekend. Thanks!
  
Gravatar # re: polish notation
by Blaise Pascal at 8/21/2011 10:59 PM

Why does a language, that uses polish notation (+ 3 2 instead of 3+2) still need an overuse of brackets?

Two reasons: syntactic consistency and variable arity.

Polish notation does not need parenthesis as long as every operator is of known arity. In lisps, the arity of many functions (including +) is variable, so there is no way to know, without the functional equivalent to parenthesis, to know when the arguments to a function are complete. So parenthesis are at least sometimes necessary.

Syntactic consistency ensures that they are always used and never optional. It makes it real easy to parse, and it isn't that hard to get used to them with a little exposure.
  
Comments have been closed on this topic.
Scott Allen
Posts - 869
Comments - 4493
Stories - 14