Intro to Clojure(script)

Michelle Lim + Tyler Perkins // Code for Denver // 3-20-17


It all starts with an expression

(function x y z)

*do function to x, y, and z*

Example 1:

(+ 1 2 3)

*adds 1, 2, and 3*

=> 6

returns six

Example 2:

(= 1 2)

*the equality of 1 and 2 is…*

=> false

returns false

Example 3:

(if (= 1 1) "yes" "no")

*if (= 1 1) is true, return “yes”, otherwise return “no”*

=> "yes"

returns the third item

Expressions within expressions

Example 4:

(= (+ 1 2 3) 5)

Complete the expression below

(+ 1 2 3)

Example 5:

(if (= (+ 1 2 3) 5) "yes" "no")

Complete the expression below

(= (+ 1 2 3) 5)

Parentheses = (hugs) for your code. Perfect for nesting.


Clojure is a general purpose language, designed to be hosted on top of another language

Tools (clickable):

[live demo: codefordenver/owlet-ui]

Clojure is dialect of Lisp: the second oldest high-level programming language widely used today

Lisp timeline:

A different paradigm

Lisps are functional programming languages.

Javascript, Python, Ruby, C#, and C++ are all object-oriented programming languages.

Context: Data can be passed around by value or reference

Object-oriented approach

A system based on objects

and specific rules for how they can communicate.

“In order to safely work with the data you have to know all the places where it might be referenced. The complexity grows with the size of the application. The more code has access to a piece of data the more proverbial balls you end up having to juggle in your head.” —Clojure Distilled

Functional approach

Defines standalone actions (expressions) that are reuseable and composeable

Functions we’ll use in Programming Functionally, next

First, let’s take them for a spin. Evaluate the examples above using the editor below.

; Use my-doc to see the docs for a function:
(my-doc #'str)

cljs cheatsheet

A note on laziness

map, take, and cycle return “lazy” sequences;

with a lazy sequence, the function returns both an item and the next function in the sequence,

providing a natural way to produce infinite data structures..!

Next: Programming Functionally →