Getting Started with Clojure in .net
Before getting started here is the pre-requisites for installing ClojureCLR
- Microsoft .NET Framework Version 3.5 SP1
- Visual Studio 2008 SP1
Dynamic Language Runtime
These are the steps followed by me for the new installation
- Created a folder called ClojureCLR
- Downloaded the Dynamic Language Runtime and extracted the DLR_Main folder into the above folder
- Compiled the DLR by opening the visual studio solution file inside DLR_Main
- Downloaded the ClojureCLR by using the git (Windows version can be downloaded here) and Opened the ClojureCLR.sln file
- Added references for NUnit and Rhinomocks (with castle dependencies download from here) Clojure.Tests project
- Set Clojure.Main as startup project and then Crl + F5 or F5 will start the interactive Clojure section
Clojure is dynamic i.e,
- Clojure can be embedded in .net applications and can be interacted with
- Can be used as a scripting language inside .net
- Can be interacted with the primary programming interface called Read-Eval-Print (REPL) loop
- Supports dynamic compilation
Part of clojure called Reader will read the clojure program in chunks called forms and translates them in to clojure data structures. when run for the first time, the clojure REPL will display the following prompt.
Clojure 1.1.0-alpha-SNAPSHOT user=>
Prompt displays the default namespace and is ready to evaluate the clojure programs. Let's feed it with a number.
Clojure 1.1.0-alpha-SNAPSHOT user=> 42 1 42 user=> (+ 2 3)2 5
Number literal 1 is a Form in clojure and evaluates to itself. Let's feed the REPL with a function that calculates the sum of two numbers 2. Several deviations from the imperative programming languages are evident from this single expression.
- All program codes are written in parenthesized lists
- A function call is written as a list with the function or operator's name (+ in the above code) first, and the arguments following (2 and 3 in above code)
- White space is the commas in Clojure (2 3 in the above code as arguments instead of 2, 3)
Clojure 1.1.0-alpha-SNAPSHOT user=> (+ 4 6) 3 10 user=> (- 5 4) 4 1 user=> (/ 2.0 3) 5 0.666666666666667 user=> (* 5 6) 6 30 user=> (inc 4) 7 5 user=> (dec 4) 8 3 user=> (quot 12 5) 9 2 user=> (rem 12 5) 10 2 user=> (min 2 3 4 1) 11 1 user=> (max 2 3 4 1) 12 4
Add, Subtract, Multiply, Divide, Increment, Decrement, quot[ient] of dividing numerator by denominator, remainder of dividing numerator by denominator, least of the nums and the greatest of the nums are the computations supported by the clojure on Numbers. In addition to the bitwise operations the numbers support the following comparison operations.
Clojure 1.1.0-alpha-SNAPSHOT user=> (== 1 1) True user=> (< 1 2) True user=> (<= 3 4) True user=> (> 3 2) True user=> (>= 4 3) True user=> (zero? 4) False user=> (pos? -2) False user=> (neg? -2) True
Other data structures will be covered in future posts.