Alex Rivera | Logout

clojure vs c# language

Asked 2011-06-08T13:33:25.850
17

I am still trying to understand the clear benefits of clojure. I understand that it is a dynamic, [almost] purely functional language that lends itself well to unit testing, concurrency and rapid development.

I was watching this presentation by its founder Rich Hickey.

Around the 25th minute mark, you can see a lot of wonderful ways in which clojure makes programming easy by ability to define lists, and vectors dynamically:

( 123) [123]

ability to use those dynamically defined vectors as functions

([ 123] 0)

ability to call functions on newly defined strings

(.ToUpperCase "foo")

so as Rich puts it, "syntax for the literals is the syntax for the language".. beautiful.

but isn't it possible to do this style of programming in .net 4.0? (albeit more chatty)

(new[] {1,2,3})[0]
"foo".ToUpper()

etc.. .net could also be used to program in a functional way (although it is not enforced)

these are trivial examples, but seems like this is the basis of clojure. What are some features that set clojure more apart, and make it a better choice in some scenarios?

Edit
Report

1 Answer

23

I personally think that Clojure's syntax is very effective, concise and even beautiful once you get the hang of it. However in my view that isn't the most compelling reason to select a language.

My personal reasons for preferring Clojure as my "general purpose development language of choice" after many years of Java and C# are:

  • Macro metaprogramming - Clojure is a Lisp and inherits the Lisp family "Code is Data" motto. Because the language itself is written in a form that is itself a data structure within the language (this is called Homoiconicity), it's trivial to write code that generates code in whatever form you choose . You basically never need to worry about "Design Patterns" ever again- if there's something missing in the language that you want to use, you just extend the language with a macro and move on.....

  • Dynamic by default - Clojure is a dynamic language by default, which means that it automatically "does the right thing" in a flexible way for most tasks. This has the effect of making you more productive. Examples are automatic overflow to BigInteger arithmetic, being able to put any object types you like in a collection, never needing to declare parameter types etc. At the same time, you can specify type information to improve performance if needed (by directly using primitives or providing type hints for example) - so you can get really fast performance when required.

  • Emphasis on functional programming with persistent, immutable data structures and lazy evaluation - in particular all the core Clojure library is designed to support this style of development by default. Once you get the hang of it (I admit it's not easy...) FP is exceptionally powerful. While you can emulate "FP-style" code in almost any language, you only really get the full advantages

answered 2011-06-09T12:15:18.270

Your Answer