A simple guide to a simple programming language
WORK IN PROGRESS
Programming Language
Based on Lisp (1958) - John McCarthy
Lisp is short for "List Processor"
A list processor reads a list of values and processes it
(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:
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
add ( 10 , 30 )
↑ ↑ ↑
value value value
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!
52 -1.5 2/3
"I'm a string"
\H \i
; start with one or more semicolons
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
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
(defn add [x y]
(+ x y))
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
(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.