Functional Programming

Clojure is a functional programming language. It provides the tools to avoid mutable state, provides functions as first-class objects, and emphasizes recursive iteration instead of side-effect based looping. Clojure is impure, in that it doesn't force your program to be referentially transparent, and doesn't strive for 'provable' programs. The philosophy behind Clojure is that most parts of most programs should be functional, and that programs that are more functional are more robust.


First-class functions

fn creates a function object. It yields a value like any other - you can store it in a var, pass it to functions etc.


(def hello (fn [] "Hello world"))
-> user/hello
(hello)
-> "Hello world"


defn is a macro that makes defining functions a little simpler.

Clojure supports arity overloading in a single function object, self-reference, and variable-arity functions using &:


;trumped-up example
(defn argcount
        ([] 0)
        ([x] 1)
        ([x y] 2)
        ([x y & more] (+ (argcount x y) (count more))))
-> user/argcount
(argcount)
-> 0
(argcount 1)
-> 1
(argcount 1 2)
-> 2
(argcount 1 2 3 4 5)
-> 5

You can create local names for values inside a function using let. The scope of any local names is lexical, so a function created in the scope of local names will close over their values:


(defn make-adder [x]
      (let [y x]
        (fn [z] (+ y z))))
(def add2 (make-adder 2))
(add2 4)
-> 6


Locals created with let are not variables. Once created their values never change!


Immutable Data Structures

The easiest way to avoid mutating state is to use immutable data structures. Clojure provides a set of immutable lists, vectors and maps. Since they can't be changed, 'adding' or 'removing' something from an immutable collection means creating a new collection just like the old one but with the needed change. Persistence is a term used to describe the property wherein the old version of the collection is still available after the 'change', and that the collection maintains its performance guarantees for most operations. Specifically, this means that the new version can't be created using a full copy, since that would require linear time. Inevitably, persistent collections are implemented using linked data structures, so that the new versions can share structure with the prior version. Singly-linked lists and trees are the basic functional data structures, to which Clojure adds a hash map and vector both based upon array mapped hash tries. The collections have readable representations and common interfaces:


(let [vec [1 2 3 4]
      map {:fred "ethel"}
      lst (list 4 3 2 1)]
    (list
        (conj vec 5)
        (assoc map :ricky "lucy")
        (conj lst 5)
        ;the originals are intact
        vec
        map
        lst))
-> ([1 2 3 4 5] {:ricky "lucy", :fred "ethel"} (5 4 3 2 1) [1 2 3 4] {:fred "ethel"} (4 3 2 1))


Applications often need to associate attributes and other data about data that is orthogonal to the logical value of the data. Clojure provides direct support for this metadata. Symbols, and all of the collections, support a metadata map. It can be accessed with the meta function (reader macro ^). Metadata does not impact equality semantics, nor will metadata be seen in operations on the value of a collection. Metadata can be read, and can be printed.


(def vec [1 2 3])
(def attributed-vec (with-meta vec {:source :trusted}))
(:source ^attributed-vec)
-> :trusted
(eql? vec attributed-vec)
-> true


Extensible Abstractions

Clojure uses Java interfaces to define its core data structures. This allows for extensions of Clojure to new concrete implementations of these interfaces, and the library functions will work with these extensions. This is a big improvement vs. hardwiring a language to the concrete implementations of its data types.


A good example of this is the seq interface. By making the core Lisp list construct into an abstraction, a wealth of library functions are extended to any data structure that can provide a sequential interface to its contents. All of the Clojure data structures can provide seqs. Seqs can be used like iterators or generators in other languages, with the significant advantage that seqs are immutable and persistent. Seqs are extremely simple, providing a first function, which return the first item in the sequence, and a rest function which returns the rest of the sequence, which is itself either a seq or nil.


(let [vec [1 2 3 4]
      map {:fred "ethel" :ricky "lucy"}
      lst (list 4 3 2 1)]
    [(first vec)
     (rest vec)
     (keys map)
     (vals map)
     (first lst)
     (rest lst)])
-> [1 (2 3 4) (:ricky :fred) ("lucy" "ethel") 4 (3 2 1)]


Many of the Clojure library functions produce and consume seqs lazily:


;cycle produces an 'infinite' seq!
(take 15 (cycle [1 2 3 4]))
-> (1 2 3 4 1 2 3 4 1 2 3 4 1 2 3)


You can define your own lazy seq-producing functions using the lazy-cons macro, which takes the first item in the sequence and an expression which will be called on demand to produce the rest of the sequence. Here's take:


(defn take [n coll]
  (when (and (pos? n) (seq coll))
    (lazy-cons (first coll) (take (dec n) (rest coll)))))


Recursive Looping

In the absence of mutable local variables, looping and iteration must take a different form than in languages with built-in for or while constructs that are controlled by changing state. In functional languages looping and iteration are replaced/implemented via recursive function calls. Many such languages guarantee that function calls made in tail position do not consume stack space, and thus recursive loops utilize constant space. Since Clojure uses the Java calling conventions, it cannot, and does not, make the same tail call optimization guarantees. Instead, it provides the recur special operator, which does constant-space recursive looping by rebinding and jumping to the nearest enclosing loop or function frame. While not as general as tail-call-optimization, it allows most of the same elegant constructs, and offers the advantage of checking that calls to recur can only happen in a tail position.


(defn my-zipmap [keys vals]
  (loop [map {}
         ks (seq keys)
         vs (seq vals)]
    (if (and ks vs)
        (recur (assoc map (first ks) (first vs))
               (rest ks)
               (rest vs))
       map)))
(my-zipmap [:a :b :c] [1 2 3])
-> {:b 2, :c 3, :a 1}
  

Copyright © Rich Hickey