8. What is Clojure?
Clojure is a dynamic, strongly
typed, functional, high
performance implementation
of Lisp on the JVM (and CLR
and yes JavaScript)
9. What is Lisp?
LISP stands for LISt Processing.
Designed by John McCarthy.
Implementation of lambda
calculus.
Oldest Language - Circa 1958
Uses Lots of ( )s - Polish Prefix
notation
Known for AI, DSLs etc
11. What is lambda calculus?
• Formal system computation by
for expressing
in Computer Science
functions
• Consists of λ expressions
• variables v1, v2, ..., vn, ...
• the abstraction symbols λ and .
• parentheses ( )
• Examples
• λx.x — is a function taking an argument x,
and returning x Alonzo Church
• f x — is a function f applied to an argument x
12. Clojure History
• Project led by Rich Hickey
• 1.0 in 2007
• 1.1 in 2009
• 1.2 in 2010
• 1.3 in 2011
• 1.4 in 2012
• 1.5 released in March 2013
• Maintained on Github
• contains one jar file clojure.jar
• You can get it from Maven repo
21. Why Clojure?
• Good at working with Data
• Rich literal types like arrays, sets, data types
• Good at expressing algorithms
• Functional
• Great for concurrency
• Immutable data structure
• Innovative approach to state - STM
33. Functional Programming
A pure Function
don’t
...look outside their box
Arguments (Perform ...modify anything, anywhere
...print messages to the user
calculations, may ...write to disk
Return Value call other pure i.e. No Side Effects
functions) E.g. f(x) = x * x + 1
A impure Function
34. Functional Programming
A pure Function
don’t
...look outside their box
Arguments (Perform ...modify anything, anywhere
...print messages to the user
calculations, may ...write to disk
Return Value call other pure i.e. No Side Effects
functions) E.g. f(x) = x * x + 1
A impure Function
Arguments Read External State
(perform
Return Value calculations) Write External State
(Side Effect)
37. Functional
Programming
• Project Euler (http://projecteuler.net)
• Problem 1: If we list all the natural numbers
below 10 that are multiples of 3 and 5, we
get 3,5,6,9. The sum of these mutiples is 23.
38. Functional
Programming
• Project Euler (http://projecteuler.net)
• Problem 1: If we list all the natural numbers
below 10 that are multiples of 3 and 5, we
get 3,5,6,9. The sum of these mutiples is 23.
Find the sum of all the multiples of 3 and 5
below 1000.
45. Project Euler: filter
• Next something that returns a list of
selected values from a larger list
(filter even? (range 5)) => (0 2 4)
• filter function takes a predicate and a
sequence (aka collection)
46. Project Euler: reduce
• Finally something to add up these numbers
• reduce takes a function and a sequence.
Applies the function to the first two
elements of the sequence, then applies the
function to the result and third item and so
on
(reduce + (range 6)) ==> 15
47. Project Euler:Completed
program
(defn div_by_3_or_5? [n]
(or (zero? (rem n 3))
(zero? (rem n 5))
)
)
(reduce +
(filter div_by_3_or_5?
(range 1000)
)
)