Clojure tutorials with klipse

I’ve attended London Clojure Dojo today, and an overwhelmingly appealing option was something with klipse.

So here’s an attempt.

Note that each clojure block is editable. And will instantly evaluate as you type (just that sexp). A single repl is shared by all the blocks on this page. You need to modify a block to run the block.


(+ 1 4)

Define a function that doubles it’s argument.

(defn dble [n] (* n 2))

Call the function from above!

(dble 3)

How do I do string interpolation? You don’t, just call str on a list

(str "Something " "about " "the " "way")
(defn hello [name] (str "hello, " name))
(hello "john")

A clojure function will evaluate to it’s last item

(defn broken-hello [name] "hello, " name)
(broken-hello "john")

How about conditionals?

(defn hate-john [name] (if (= name "john") "Go away john" (str "Hello, " name) ))
(hate-john "john")
(hate-john "jerry")

True and false

Truthy things

(= true true)
(= false true)
(= false false)
(= false nil)
(= nil nil)

A single quote before parens says “Don’t treat this list as a function call, it is really a list!”. An empty list is ‘()

(= '() '())

Vectors are like arrays in other languages, but without the horrific mutability stuff. You want them instead of lists when you want fast-indexing to the middle of the datastructure. Empty Vectors are ‘[]. Note that we’ve already seen vectors above, clojure uses them for parameter lists in functions!

(= '[] '[])

Let’s compare a non-empty list to a non-empty vector

(= '[123 456 789] '(123 456 789))
Lance Paine 23 March 2016
blog comments powered by Disqus