## Wednesday, November 9, 2011

### Functions of Functions

This write-up follows along with Brian Harvey's CS61A 2008 SICP class, lecture 3.

Note that these are not good examples of idiomatic Clojure coding style, but more of an introduction to functional programming using Clojure.

basic definition of a function
```(defn sq1 [x] (* x x))
(def sq (fn [x] (* x x) ) )
```

an anonymous function (lambda) with a name in scheme ->(define sq (lamba) (x) (* x x)) and the clojure translation is
```(def sq #(* % %) )
```

the truly anonymous function in scheme -> (lambda (x) (* x x)) and the clojure translation is
```#(* % %)
```

scheme -> (define f (lambda (x y) (+ (* 2 x) y))) and the clojure translation is
```(def f #(+ (* 2 %1) %2))
```

a function that takes a function in scheme -> (define (compose f g) (lambda (x) (f (g x)))) and the clojure translation is
```(defn compose [f g] #(f (g %)))
```

going further in scheme -> (define sec (compose first bf)) and the clojure translation is
```(def sec (compose first rest))
(sec '(i want to tell you))
```

another compound function example using the (compose) function defined above
```(defn twice [f] (compose f f))
(def fourth (twice sq))
(fourth 3)
```