Monday, May 16, 2011

Early Explorations in Clojure

Having never programmed in Lisp before, and never really been able to start making sense of the code snippets I've seen, this is all very new to me.  However, I've got the following figured out:

Function calls are surrounded with parenthesis and arguments are separated by spaces not commas.  String literals are surrounded by quotes.  

Print "hello world" to stdout:
(println "hello world")

Single quotes cannot be used for string literals though
(println 'hello world')
java.lang.Exception: Unmatched delimiter: )

Clojure provides access to the core java libraries - albeit with some slight syntax changes...

Java:
System.out.println(System.getProperty("user.dir"));


Clojure:
(println (System/getProperty "user.dir"))

You can use defn to declare a function.  Notice the argument list in square brakets. 
(defn foo [bar baz] (str bar "," baz))
(foo 1 2)
==> "1,2"

The str function concatenates the strings passed.


No comments:

Post a Comment