Clojure for non-programmers

A simple guide to a simple programming language

WORK IN PROGRESS

Programming Language

 

 Based on Lisp (1958) - John McCarthy

What is clojure?

Lisp? It's been around for a while...

Lisp is short for "List Processor"

A list processor reads a list of values and processes it

What is lisp?

Ok, what's a list then?

(1 2 3 4)

 

(add 1 2 3 4)

 

(print "I'm a list" :too "holding any" \d \a \t \a)

 

(add 1 2 (add 3 4 (add 5 6 7)))

Lists are processed by reducing them to a single value through evaluation. Here's how:

  • the first value of a list is treated as a function
  • the remaining values are arguments of this function
  • Lisp invokes the function with arguments and produces a value

How are lists processed?

Ok, you lost me there. What is a function?

Oh no, are we doing math?

3 + 4 = 7

x + y = ?

A function taking two arguments:

f(x,y) := x + y

 

f(1,1) = 2

f(10,30) = 40

 

add(x,y) := x + y

 

add(2, add(10,30)) = 42

add(add(add(2,8), 30), 2) = 42

What is a function?

                        add  (     10   ,     30    )  

                          ↑               ↑            ↑

                          value      value      value

Calling Functions in lisp

                     In Lisp, this is simply a list of 3 values

                      (add        10         30)

 

                     add(2, add(10,30))

                                     ↓

                    (add 2 (add 10 30))

 

Lisp evaluates lists to calculate a value

 

Lists in that context are also called s-expressions or just expressions

 

Values are calculated by function application
 

We also say, since the code we write are lists and lists are data that code is data!

Quick Recap

A bit more syntax: Literals

Numbers

52         -1.5       2/3

Strings + Characters

"I'm a string"

\H \i

 

Comments

; start with one or more semicolons

BOOLEAN

NOTHING

nil

true

false

A name that refers to something else, like a value.

add    ; symbol that refers to a function

+        ; most characters are allowed as symbols

price-per-watt

SYMBOL

Keywords

Constants that evaluate to themselves (like numbers), start with a colon

:first-name

:last-name

:borrower/first-name

Data structures that hold multiple values. Most used ones are:

List         (1 2 3 4)

Vector    [1 2 3 4]

Maps      {:first-name "Han"

                  :last-name "Solo"}

Sets         #{1 2 3 4}

Note: These are also values

Collections

🎉 You now know 90% of Clojure syntax 🎉

(defn add [x y]

   (+ x y))

CAN I HAZ MY OWN Functions?

What does this fn return when called?
It's the value of the last expression of a function!

(add 10 32)

⇒ (+ 10 32)

⇒ 42

 

(add (add 4 6) 32)  ⇒ (add (+ 4 6) 32)

⇒ (add 10 32) ⇒ (+ 10 32) ⇒ 42

More functions

(defn hello-world [name]

   (str "Hello " name))

Dynamic development via the Read-Eval-Print Loop (REPL)

Work inside your application

Very fast feedback cycle

 

Functional Programming

Functions are the main building blocks

 

Immutable data structures

Data structures like collections are values and can't be changed.

big ideas of clojure

Made with Slides.com