Lisp Introduction

Dr. Neil . Dantam

CSCI-534, Colorado School of Mines

Spring 2020

Dantam (Mines CSCI-534) Lisp Spring 2020 1 / 80 What is Lisp? Definition (Lisp) A family of programming languages based on s-expressions.

Data Structure “Math” NIL

( defun factorial NIL 1 if n = 0 n! = n ∗ (n − 1)! if n 6= 0 n 1 NIL

if 0 NIL Code = n ( defun f a c t ( n ) NIL ( i f (= n 0) n * NIL 1 (∗ n ( f a c t (− n 1 ) ) ) ) ) factorial 1 NIL - n

Dantam (Mines CSCI-534) Lisp Spring 2020 2 / 80 Homoiconic Code is Data

Code Data Structure

NIL ( defun f a c t ( n ) defun factorial NIL ( i f (= n 0) n 1 (∗ n ( f a c t (− n 1 ) ) ) ) ) 1 NIL

if 0 NIL = n

NIL n * NIL

factorial 1 NIL

- n

“data processing” ⇐⇒ “code processing”

Dantam (Mines CSCI-534) Lisp Spring 2020 3 / 80 Introduction

Why study lisp? Outcomes I Understand Lisp abstractions I Functional Programming: I Symbols I Planning algorithms often are I S-expressions functional/recursive I First-class functions Lisp has good support for functional I (closures) programming. Implement Lisp programs in I Symbolic Computing: I functional style I Planning algorithms must often process symbolic expressions I (Review differential calculus I Lisp has good support for symbolic processing and numerical methods) I Understanding the abstractions in Lisp will make you a better programmer.

Dantam (Mines CSCI-534) Lisp Spring 2020 4 / 80 The Lisp Learning Process Yee-haw!

start “Lisp is weird!”

“I dislike Learn to think differences!” give symbolically? not yet up yes end “Lisp is awesome!” end Lisp might feel like it’s “from outer space” (at first).

The symbolic view of programming is different, often useful.

Dantam (Mines CSCI-534) Lisp Spring 2020 5 / 80 Lots of Irritating Silly Parenthesis? “LISP has jokingly been described as ‘the most intelligent way to misuse a com- puter’. I think that description a great compliment because it transmits the full flavour of liberation: it has assisted a number of our most gifted fellow humans in thinking previously impossible thoughts.” [emphasis added]

https://xkcd.com/297/

Dantam (Mines CSCI-534) Lisp Spring 2020 6 / 80 by Example Outline

Common Lisp by Example Basics Recursion First-class functions Higher-order Functions

Implementation Details

Programming Environment

Dantam (Mines CSCI-534) Lisp Spring 2020 7 / 80 Common Lisp by Example Basics Booleans and Equality

“Math” Lisp Notes False nil equivalent to the empty list () True t or any non-nil value ¬a (not a) a = b (= a b) numerical comparison a = b (eq a b) same object a = b (eql a b) same object, same number and type, or same character a = b (equal a b) eql objects, or lists/arrays with equal elements a = b (equalp a b) = numbers, or same character (case-insensitive), or recursively-equalp cons cells, arrays, structures, hash tables a 6= b (not (= a b)) similarly for other equality functions

Dantam (Mines CSCI-534) Lisp Spring 2020 8 / 80 Common Lisp by Example Basics Example: Lisp Equality Operators

I (= 1 1) t I (= "a" "a") error I (eq 1 1) t I (eq "a" "a") nil integer float I (eql "a" "a") nil z}|{ z}|{ (= 1 1.0 ) I t I (equal "a" "a") t integer float z}|{ z}|{ I (equal "a" "A") nil I (eq 1 1.0 ) nil I (equalp "a" "A") t integer float z}|{ z}|{ I (not t) nil I (eql 1 1.0 ) nil (not nil) t integer float I z}|{ z}|{ I (not "a") nil I (equal 1 1.0 ) nil integer float z}|{ z}|{ I (equalp 1 1.0 ) t

Dantam (Mines CSCI-534) Lisp Spring 2020 9 / 80 nil nil nil t t nil nil t nil t t

Common Lisp by Example Basics Exercise: Lisp Equality Operators

(eq (list ”a””b”) (list ”a””b”)) I (not 0) I (equal (list ”a””b”) (list ”a””b”)) I (not 1) I I (eq t (not nil)) I (eq t 1) I (eq (list ”a””b”) (list ”a””B”)) I (eq nil (not 1)) I (equal (list ”a””b”) (list ”a””B”)) I (eq nil (not "a")) I (equalp (list ”a””b”) (list ”a””B”))

Dantam (Mines CSCI-534) Lisp Spring 2020 10 / 80 Common Lisp by Example Basics Inequality

“Math” Lisp a < b (< a b) a ≤ b (<= a b) a > b (> a b) a ≥ b (>= a b)

Dantam (Mines CSCI-534) Lisp Spring 2020 11 / 80 Common Lisp by Example Basics Function Definition

DEFUN Defines a new function in the global environment.

(defun FUNCTION-NAME ARGUMENTS BODY...)

Pseudocode Common Lisp function name function arguments Procedure increment(n) z }| { z}|{ 1 return n + 1; (defun increment (n) (+ n 1)) | {z } result

Dantam (Mines CSCI-534) Lisp Spring 2020 12 / 80 ( defun sinc (theta) (/( s i n t h e t a ) t h e t a ) )

Common Lisp by Example Basics Exercise: Function Definition Sine Cardinal

sin θ sinc θ = θ

Pseudocode Common Lisp

Procedure sinc(θ) 1 return sin(θ)/θ;

What’s wrong (mathematically) with this definition?

Dantam (Mines CSCI-534) Lisp Spring 2020 13 / 80 Common Lisp by Example Basics Limit of sinc θ

d sin θ l’Hôpital’s rule dθ sin θ cos θ lim lim lim 1 θ→0 θ θ→0 d θ→0 1 dθ θ

1

0.5

0

−15 −10 −5 0 5 10 15

Dantam (Mines CSCI-534) Lisp Spring 2020 14 / 80 Common Lisp by Example Basics Conditional IF IF Conditional execution based on a single test: (if TEST THEN-EXPRESSION [ELSE-EXPRESSION])

Pseudocode Common Lisp Procedure even?(n) (defun even? (n) test 1 if 0 = mod(n, 2) then z }| { (if (= 0 (mod n 2)) 2 return true; then clause 3 else z}|{t 4 return false; NIL )) |{z} else clause

Dantam (Mines CSCI-534) Lisp Spring 2020 15 / 80 ( defun sinc (theta) (if (= 0 theta) 1 (/( s i n t h e t a ) t h e t a ) ) )

Common Lisp by Example Basics Exercise: Conditionals IF ( 1 if θ = 0 sinc(θ) = sin θ θ if θ 6= 0

Pseudocode Common Lisp

Procedure sinc(θ) 1 if 0 = θ then 2 return 1; 3 else 4 return sin(θ)/θ;

Dantam (Mines CSCI-534) Lisp Spring 2020 16 / 80 Common Lisp by Example Basics Taylor Series

Represent function f (x) as infinite sum of derivatives around point a:

f 0(a) f 00(a) f 000(a) f (x) = f (a) + (x − a) + (x − a) + (x − a) + ... 1! 2! 3! ∞ ! X f (n)(a) = (x − a)n n! n=0

Polynomial approximation of functions

Dantam (Mines CSCI-534) Lisp Spring 2020 17 / 80 Common Lisp by Example Basics Sinc Taylor Series

sin θ θ2 θ4 θ6 θ8 θ10 = 1 − + − + − + ... θ 6 120 5040 362880 39916800 2 n = ∞ n = 0 n = 2 1 n = 4 n = 6 n = 8 0 n = 10

−1 −15 −10 −5 0 5 10 15

Dantam (Mines CSCI-534) Lisp Spring 2020 18 / 80 Common Lisp by Example Basics Conditional COND COND Conditional execution of the first clause with a true test: (cond CLAUSES...) where each clause is of the form: (TEST EXPRESSIONS...)

Pseudocode Common Lisp Procedure sign(n) (defun sign (n) test result 1 if n > 0 then return 1 ; z }| { z}|{ (cond ((> n 0) 1 ) 2 else if n < 0 then return −1 ; test result 3 else return 0 ; z }| { z}|{ ((< n 0) −1 ) test result z}|{ (z}|{t 0 ))) Dantam (Mines CSCI-534) Lisp Spring 2020 19 / 80 ( defun sinc (theta) ( cond ((= 0 theta) 1) ((< (∗ theta theta) .00001) (+ 1 (/( expt t h e t a 2) −6) (/( expt theta 4) 120))) ( t (/ ( s i n t h e t a ) t h e t a ) ) ) )

Common Lisp by Example Basics Exercise: Conditionals COND sin θ θ2 θ4 = 1 − + + ... θ 6 120 Pseudocode Common Lisp

Procedure sinc(θ) 1 if 0 = θ then 2 return 1; 3 else if θ2 < .00001 then 4 return 1 − θ2/6 + θ4/120; 5 else 6 return sin(θ)/θ;

Dantam (Mines CSCI-534) Lisp Spring 2020 20 / 80 Common Lisp by Example Recursion Recursion

Example: Factorial ( 1 if n = 0 n! = Recursion: A function (or other object) defined n ∗ (n − 1)! if n 6= 0 in terms of itself Base Case: Terminating condition Procedure factorial(n) Recursive Case: Reduction towards the base case 1 if 0 = n then // base case 2 return 1; 3 else // recursive case 4 return n ∗ factorial(n − 1);

Dantam (Mines CSCI-534) Lisp Spring 2020 21 / 80 Common Lisp by Example Recursion Example: Factorial

( 1 if n = 0 n! = n ∗ (n − 1)! if n 6= 0

Pseudocode Common Lisp

Procedure factorial(n) ( defun factorial (n) 1 if 0 = n then // base case ( i f (= n 0) 2 return 1; 1 3 else // recursive case (∗ n (factorial (− n 1 ) ) ) ) ) 4 return n ∗ factorial(n − 1);

Dantam (Mines CSCI-534) Lisp Spring 2020 22 / 80 Common Lisp by Example Recursion Factorial Execution Trace

factorial(3)

Procedure factorial(n) 3*factorial(2) 1 if 0 = n then // base case 2 return 1; 3*(2*factorial(1)) 3 else // recursive case 4 return n ∗ factorial(n − 1); 3*(2*(1*factorial(0)))

3*(2*(1*1))

Dantam (Mines CSCI-534) Lisp Spring 2020 23 / 80 Common Lisp by Example Recursion Factorial Call Stack

fact(0) fact(1) fact(1) fact(2) fact(2) fact(2) fact(3) fact(3) fact(3) fact(3) fact(3) 3*fact(2) 3*(2*fact(1)) 3*(2*(1*fact(0)))

fact(0) 1 fact(1) fact(1) 1 fact(2) fact(2) fact(2) 2 fact(3) fact(3) fact(3) fact(3) 6 3*(2*(1*1)) 3*(2*1) 3*2 6

Dantam (Mines CSCI-534) Lisp Spring 2020 24 / 80 Common Lisp by Example Recursion Exercise: Recursion, Fibonacci Sequence

(1, 1, 2, 3, 5, 8, 13, 21, 34, 55,...)  1 if n = 0  fib(n) = 1 if n = 1 fib(n − 1) + fib(n − 2) if n ≥ 2

Dantam (Mines CSCI-534) Lisp Spring 2020 25 / 80 Procedure fib(x) 1 if 0 = n then return 1 ; ( defun f i b ( n ) 2 else if 1 = n then return 1 ; ( cond ((= n 0) 1) 3 else ((= n 1) 1) 4 return fib(n − 1) + fib(n − 2); ( t (+ ( f i b (− n 1) ) ( f i b (− n 2 ) ) ) ) ) )

Common Lisp by Example Recursion Exercise: Recursion, Fibonacci Sequence continued

Pseudocode Common Lisp

Dantam (Mines CSCI-534) Lisp Spring 2020 26 / 80 Function accumulate(S) 1 if S then // Recursive Case 2 return car(S) + accumulate (cdr (S)); 3 else // Base Case 4 return 0;

Common Lisp by Example Recursion Exercise: Recursion, Accumulate

Iterative Recursive

Function accumu- late(S) 1 a ← 0; 2 i ← 0; 3 while i < |S| do 4 a ← a + Si ; 5 return a;

Dantam (Mines CSCI-534) Lisp Spring 2020 27 / 80 Recursive Implementation of Accumulate Function accumulate(S) 1 if S then // Recursive Case ( defun accumulate ( l i s t ) 2 return ( i f l i s t car(S) + accumulate (cdr (S)); ;; recursive case 3 else // Base Case (+ ( c a r l i s t ) 4 return 0; (accumulate (cdr l i s t ))) ;; base case 0 ))

Common Lisp by Example Recursion Exercise: Recursion, Accumulate Lisp Code

Dantam (Mines CSCI-534) Lisp Spring 2020 28 / 80 (accumulate ’(1 2 3))

Recursive Implementation of Accumulate (+ 1 (accumulate ’(2 3))) ( defun accumulate ( l i s t ) ( i f l i s t ;; recursive case (+ 1 (+ 2 (accumulate ’(3)))) (+ ( c a r l i s t ) (accumulate (cdr l i s t ))) ;; base case (+ 1 (+ 2 (+ 3 (accumulate nil)))) 0 ))

(+ 1 (+ 2 (+ 3 0)))

Common Lisp by Example Recursion Exercise: Recursion, Accumulate Execution Trace

Dantam (Mines CSCI-534) Lisp Spring 2020 29 / 80 Common Lisp by Example First-class functions First-class functions

Definition: First-class functions A has first-class functions when it treats functions like any other variable or object. First-class functions can be: I Assigned to variables I Passed as arguments to other functions I Returned as the result of other functions

Dantam (Mines CSCI-534) Lisp Spring 2020 30 / 80 Common Lisp by Example First-class functions Local Variables LET, LET* LET and LET* create and initialize new local variables. LET operates in “parallel” and LET* operates sequentially.

Example (LET) Example (LET*)

( l e t ( ( a 1) ) ( l e t ( ( a 1) ) ( l e t ( ( a 2) ( l e t ∗ ( ( a 2) ( b a ) ) ( b a ) ) ( p r i n t ( l i s t a b ) ) ) ) ( p r i n t ( l i s t a b ) ) ) )

Output Output

(2 1) (2 2)

Dantam (Mines CSCI-534) Lisp Spring 2020 31 / 80 Common Lisp by Example First-class functions Closure Definition (Closure) A function and an associated set of variable definitions. From “closed expression.”

C Function Pointer Java Class /∗ D e f i n i t i o n ∗/ // Definition s t r u c t c o n t e x t { class Adder { i n t v a l ; p u b l i c i n t a ; }; public Adder( i n t a_) { a = a_ ; i n t adder ( s t r u c t c o n t e x t ∗ cx , i n t x ) { } r e t u r n cx−>a + x ; p u b l i c i n t c a l l ( i n t x ) { } r e t u r n x+a ; } /∗ Usage ∗/ } s t r u c t c o n t e x t c ; c . v a l = 1 ; // Usage i n t y = adder(c,2); Adder A = new Adder(1); i n t y = A.call(2);

Dantam (Mines CSCI-534) Lisp Spring 2020 32 / 80 Common Lisp by Example First-class functions Closures in Lisp: Local Functions LABELS Defines local functions and executes body using those local functions: (labels ((FUNCTION-NAME VARIABLES FUNCTION-BODY)...) LABELS-BODY)

Example

( l e t ( ( a 1) ) ( l a b e l s ( ( adder ( x ) (+ x a ) ) ) ( adder 2 ) ) )

Output 3

Dantam (Mines CSCI-534) Lisp Spring 2020 33 / 80 Common Lisp by Example First-class functions Closures in Lisp: Anonymous Functions LAMBDA FUNCALL Defines an anonymous function: Apply a function to the provided arguments: (lambda VARIABLES FUNCTION-BODY) (funcall FUNCTION ARGUMENTS...)

Example

( l e t ( ( a 1) ) ( f u n c a l l ( lambda ( x ) (+ x a ) ) 2 ))

Output 3

Dantam (Mines CSCI-534) Lisp Spring 2020 34 / 80 Common Lisp by Example First-class functions Value and Function Namespaces

Example Value Namespace ( defun foo (x) (+ 1 x)) I Records values I Local: let, let* ( l e t ( ( foo 10)) I Global: defparameter ( p r i n t foo ) ; => 10 ( p r i n t ( foo 1 ) ) ; =>2 ( p r i n t ( foo foo ) ) ) ; => 11

Function Namespace Output I Records function definitions 10 I Local: labels, flet 2 I Global: defun 11

Dantam (Mines CSCI-534) Lisp Spring 2020 35 / 80 Common Lisp by Example First-class functions function and funcall FUNCTION FUNCALL Returns the functional value of a name: Apply a function to the provided arguments: (function NAME) The function bound to name (funcall FUNCTION ARGS ...) Return value of FUNCTION Example called on ARGS .... I (function +) Example (#’ +) I I (funcall (lambda (x) I (defun foo (x) (+ 1 x)) (+ 1 x)) #’foo 1) I (labels ((foo (x) (+ 1 x))) 2 #’foo) I (funcall #’+ 1 2) 3

Dantam (Mines CSCI-534) Lisp Spring 2020 36 / 80 Common Lisp by Example First-class functions Example Domain: Numerical Integration Runge-Kutta Methods

d Given: I Derivative: dt x(t) = f (x, t) I Initial time: t0 I Final time: tn I Initial value: x(t0) Find: x(tn)

Solution: Follow derivative along discrete time intervals ∆t from t0 to tn

Dantam (Mines CSCI-534) Lisp Spring 2020 37 / 80 Common Lisp by Example First-class functions Example: Runge-Kutta 1 (Euler’s Method)

1.2

1

0.8

xi+1 ≈ xi + ∆t ∗ f (xi , ti ) 0.6

0.4

0.2

0 0 0.2 0.4 0.6 0.8 1 1.2

Dantam (Mines CSCI-534) Lisp Spring 2020 38 / 80 Common Lisp by Example First-class functions Example: Runge-Kutta 1 (Euler’s Method) continued

xi+1 ≈ xi + ∆t ∗ f (xi , ti )

Procedure euler-step(dx, dt, x0) ( defun e u l e r − step (dx dt x0) (+ x0 1 return x + dx ∗ dt; 0 (∗ dx dt ) ) )

Dantam (Mines CSCI-534) Lisp Spring 2020 39 / 80 Common Lisp by Example First-class functions Example: Runge-Kutta 2 (Midpoint Method)

∆t ≈x˙(ti + 2 ) z }| { ∆t ∆t x ≈ x + ∆t ∗ f (x + f (x , t ), t + ) i+1 i i 2 i i 2

Procedure rk2-mid(f , t0, x0, dt) ( defun rk2−mid−step (f t0 x0 dt) 1 function ks(c, k) is ( l a b e l s 2 let ( ( ks ( c k ) 3 kt ← c ∗ dt; ( l e t ∗ ( ( kt (∗ c dt ) ) 4 x ← ( x ( e u l e r − step k kt x0))) euler-step(k, kt , x0); 5 in return f (x, t0 + kt ); ( f u n c a l l f x (+ t0 kt))))) l e t f u n c a l l 6 let ( ∗ ( ( k0 ( f x0 t0 ) ) 7 k0 ← f (x0, t0); (k1 (ks (/ 1 2) k0))) 8 k1 ← ks(1/2, k0); (+ x0 (∗ dt k1 ) ) ) ) ) 9 in return x0 + dt ∗ k1;

Dantam (Mines CSCI-534) Lisp Spring 2020 40 / 80 Common Lisp by Example First-class functions Exercise: Runge-Kutta 2 (Heun’s Method)

x˙(ti ) ≈x˙(t+∆t) ∆t z }| { ∆t z }| { x ≈ x + ∗ f (x , t )+ ∗ f (x + (∆t)f (x , t ), t + ∆t) i+1 i 2 i i 2 i i i ∆t ∆t ≈ x + k + k i 2 0 2 1 Averaging derivatives at current and next point.

Dantam (Mines CSCI-534) Lisp Spring 2020 41 / 80 ( defun rk2−heun (f t0 x0 dt) ( l a b e l s ( ( ks ( c k ) ( l e t ∗ ( ( kt (∗ c dt ) ) ( x ( e u l e r − step k kt x0))) ( f u n c a l l f x (+ t0 kt))))) ( l e t ∗ ( ( k0 ( f u n c a l l f x0 t0 ) ) (k1 (ks 1 k0))) (+ x0 (∗ (/ dt 2) (+ k0 k1))))))

Common Lisp by Example First-class functions Exercise: Runge-Kutta 2 (Heun’s Method) continued

Procedure rk2- heun(f , t0, x0, dt) 1 function ks(c, k) is 2 let 3 kt ← c ∗ dt; 4 x ← euler-step(k, kt , x0); 5 in return f (x, t0 + kt );

6 let 7 k0 ← f (x0, t0); 8 k1 ← ks(1, k0); 9 in return x0 + dt/2 ∗ (k0 + k1);

Dantam (Mines CSCI-534) Lisp Spring 2020 42 / 80 Common Lisp by Example First-class functions Exercise: Runge-Kutta 4

∆t ∆t x˙(ti ) ≈x˙(ti + 2 ) ≈x˙(ti + 2 ) ≈x˙(ti +∆t) ∆t z}|{ ∆t z}|{ ∆t z}|{ ∆t z}|{ x ≈ x + k + k + k + k i+1 i 6 0 3 1 3 2 6 3 ∆t ∆t ≈ x + (k + k ) + (k + k ) i 6 0 3 3 1 2 where:

I k0 = f (xi , ti ) (current) ∆t ∆t I k1 = f (xi + 2 k0, ti + 2 ) (midpoint) ∆t ∆t I k2 = f (xi + 2 k1, ti + 2 ) (midpoint) I k3 = f (xi + (∆t)k2, ti + ∆t) (next) Weighted average at current point, midpoint, and next point.

Dantam (Mines CSCI-534) Lisp Spring 2020 43 / 80 ( defun rk4−step (f t0 x0 dt) ( l a b e l s ( ( ks ( c k ) ( l e t ∗ ( ( kt (∗ c dt ) ) ( x ( e u l e r − step k kt x0))) ( f u n c a l l f x (+ t0 kt))))) ( l e t ∗ ( ( k0 ( f u n c a l l f x0 t0 ) ) (k1 (ks (/ 1 2) k0)) (k2 (ks (/ 1 2) k1)) (k3 (ks 1 k2))) (+ x0 (∗ (/ dt 6) (+ k0 k3)) (∗ (/ dt 3) (+ k1 k2))))))

Procedure rk4-step(f , t0, x0, dt) 1 function ks(c, k) is 2 let 3 kt ← c ∗ dt; 4 x ← euler-step(k, kt , x0); 5 in return f (x, t0 + kt );

6 let 7 k0 ← f (x0, t0); 8 k1 ← ks(1/2, k0); 9 k2 ← ks(1/2, k1); 10 k3 ← ks(1, k2); 11 in return x0 + dt/6 ∗ (k0 + k2) + dt/3 ∗ (k0 + k1);

Common Lisp by Example First-class functions Exercise: Runge-Kutta 4 continued

Dantam (Mines CSCI-534) Lisp Spring 2020 44 / 80 Common Lisp by Example First-class functions Euler (RK-1) Integration

Procedure int-rk1(f , t0, tn, dt, x0)

1 if t0 ≥ tn then // Base Case 2 return x0; ( defun int−rk1 (f t0 tn dt x0) 3 else // Recursive Case (if (>= t0 tn) 4 let x0 ( l e t ∗ ( ( dx ( f u n c a l l f x0 t0 ) ) 5 dx ← f (x0, t0); 6 x ← ( x ( e u l e r − step dx dt x0)) ( t1 (+ t0 dt ) euler-step(dx, dt, x0); ( int−rk1 f t1 tn dt x)))))) 7 t1 ← t0 + dt; 8 in return int-rk1(f , t1, tn, dt, x);

Dantam (Mines CSCI-534) Lisp Spring 2020 45 / 80 Common Lisp by Example First-class functions RK-2 Integration

Procedure int-rk2(f , t0, tn, dt, x0)

1 if t0 ≥ tn then // Base Case 2 return x0; ( defun int−rk2 (f t0 tn dt x0) 3 else // Recursive Case (if (>= t0 tn) 4 let x0 5 x ← ( l e t ( ( x ( rk2−heun f t0 x0 dt)) rk2-heun(f , t0, x0, dt); (t1 (+ t0 dt))) 6 t1 ← t0 + dt; ( int−rk2 f t1 tn dt x)))) 7 in return int-rk2(f , t1, tn, dt, x);

Dantam (Mines CSCI-534) Lisp Spring 2020 46 / 80 ( defun int−rkx (s f t0 tn dt x0) (if (>= t0 tn) x0 ( l e t ( ( x ( f u n c a l l s f t0 x0 dt ) ) (t1 (+ t0 dt))) ( int−rkx s f t1 tn dt x))))

if t0 ≥ tn then // Base Case 2 return x0; 3 else // Recursive Case 4 let 5 x ← s(f , t0, x0, dt); 6 t1 ← t0 + dt; 7 in return int-rkx(s, f , t1, tn, dt, x);

Common Lisp by Example First-class functions Exercise: Multi-method RK Integration

Procedure int-rkx(s, f , t0, tn, dt, x0) 1

Dantam (Mines CSCI-534) Lisp Spring 2020 47 / 80 Common Lisp by Example Higher-order Functions Higher-order functions

Definition: Higher-order function A function that takes another function as an argument or returns another function as its result.

Example (Passing) Example (Returning) Counterexample

Function f(g,a) Function f(a) Function f(a,b) 1 return g(42, a); 1 function g(b) is 1 return a + b; 2 return a + b;

3 return g;

Dantam (Mines CSCI-534) Lisp Spring 2020 48 / 80 Common Lisp by Example Higher-order Functions Map function

Definition (map) Apply a function to every member of an input sequence, and collect the results into the output sequence.

n n map :( X 7→ Y) × X 7→ Y | {z } |{z} |{z} function input sequence output sequence

Illustration

f output sequence z }| { input sequence MAP   f (x0), f (x1),..., f (xn−1) z }| { x0, x1,..., xn−1

Dantam (Mines CSCI-534) Lisp Spring 2020 49 / 80 Common Lisp by Example Higher-order Functions Example: Map

+1 λx . x + 1 MAP (1 + 1 2 + 1 3 + 1) (2 3 4) (1 2 3)

¬ ¬ MAP (¬true ¬false ¬true) (false true false) (true false true)

Dantam (Mines CSCI-534) Lisp Spring 2020 50 / 80 Common Lisp by Example Higher-order Functions Algorithm: Map function

Functional Map Imperative Map

Procedure map(f,s) Procedure map(f,s) 1 if empty(s) then /* s is empty */ 1 n ← length(s); 2 return NIL 2 Y ← make-sequence(n); 3 else /* s has members */ 4 return cons (f (first(s)), map(f , rest(s)); 3 i ← 0; 4 while i < n do 5 Y [i] ← f (s[i]); 6 i ← i + 1;

7 return Y ;

Dantam (Mines CSCI-534) Lisp Spring 2020 51 / 80 Common Lisp by Example Higher-order Functions Example: Map

Example (Illustration) (lambda (x) (+ x 1)) MAP (2 3 4) (1 2 3)

Example (Lisp)

(map ’ l i s t ; result type (lambda (x) (+ 1 x)) ; function ( l i s t 1 2 3 ) ) ; sequence ;; RESULT: (23 4)

Dantam (Mines CSCI-534) Lisp Spring 2020 52 / 80 Common Lisp by Example Higher-order Functions Fold-left Definition (fold-left) Apply a binary function to each member of a sequence and the prior result, starting from the left. n fold-left :(Y × X 7→ Y) × Y × X 7→ Y | {z } |{z} |{z} |{z} function init. sequence result

Illustration f f ... y FOLD-LEFT xn f ... (x0 x1 ... xn−1) y x0

Dantam (Mines CSCI-534) Lisp Spring 2020 53 / 80 Common Lisp by Example Higher-order Functions Example: Fold-left Example (Addition)

3 + + 1 + 3 0 FOLD-LEFT + 2 6 (1 2 3) 0 1

Example (Subtraction)

−3 − − −1 − 3 −6 0 FOLD-LEFT − 2 (1 2 3) 0 1

Dantam (Mines CSCI-534) Lisp Spring 2020 54 / 80 Common Lisp by Example Higher-order Functions Algorithm: Fold-left

Imperative Functional

Function fold-left(f, y, X) Function fold-left(f, y, X) 1 i ← 0; 1 if empty(X ) then return y; 2 while i < |X | do 2 else 0 3 y ← f (y, Xi ); 3 let y ← f (y, first(X)) in 0 4 i ← i + 1; 4 return fold-left (f , y , rest(X));

5 return y;

Dantam (Mines CSCI-534) Lisp Spring 2020 55 / 80 Common Lisp by Example Higher-order Functions Example: Fold-left in Lisp

Example (Addition) Example (Subtraction)

( reduce #’+ ( reduce #’− ’(1 2 3) ’(1 2 3) : i n i t i a l − v a l u e 0) : i n i t i a l − v a l u e 0) ;;; => (+ (+ (+0 1) 2) 3) ;;; =>( − (− (− 0 1) 2) 3) ;;; =>6 ;;; => −6

Dantam (Mines CSCI-534) Lisp Spring 2020 56 / 80 ( defun r e v e r s e − f o l d ( l i s t ) function h(r, x) is ( reduce (lambda (reversed x) 2 return cons(x, r) ; ( cons x reversed)) 3 return fold-left(h, NIL, L); l i s t : i n i t i a l − v a l u e n i l ) )

Common Lisp by Example Higher-order Functions Exercise: Fold-Left Reverse

reverse (a0 a1 ... an−1 an) (an an−1 ... a1 a0)

Procedure reverse(L)

1

Dantam (Mines CSCI-534) Lisp Spring 2020 57 / 80 Common Lisp by Example Higher-order Functions Fold-right Definition (fold-right) Apply a binary function to each member of a sequence and the prior result, starting from the right. n fold-right :(X × Y 7→ Y) × Y × X 7→ Y | {z } |{z} |{z} |{z} function init. sequence result

Illustration f f ... y FOLD-RIGHT x0 ... f (x0 x1 ... xn−1) xn−1 y

Dantam (Mines CSCI-534) Lisp Spring 2020 58 / 80 Common Lisp by Example Higher-order Functions Example: Fold-right Example (Addition)

+ + 5

0 FOLD-RIGHT 1 + 3 6 (1 2 3) 2 + 3 0

Example (Subtraction)

− − −1

0 FOLD-RIGHT 1 − 3 2 (1 2 3) 2 − 3 0

Dantam (Mines CSCI-534) Lisp Spring 2020 59 / 80 Common Lisp by Example Higher-order Functions Algorithm: Fold-right

Procedural Recursive Function fold-right(f,y,X) Function fold-right(f,y,X) 1 i ← |X | − 1; 1 if empty(X ) then return y; 2 while i ≥ 0 do 2 else 0 3 y ← f (Xi , y) ; 3 let y ← fold-right (f , y, rest(X)) in 0 4 i ← i − 1 ; 4 return f (first(X), y );

5 return y;

Dantam (Mines CSCI-534) Lisp Spring 2020 60 / 80 Common Lisp by Example Higher-order Functions Example: Fold-right in Lisp

Example (Addition) Example (Subtraction)

( reduce #’+ ( reduce #’− ’(1 2 3) ’(1 2 3) : i n i t i a l − v a l u e 0 : i n i t i a l − v a l u e 0 : from−end t ) : from−end t ) ;;; => (+1 (+2 (+3 0))) ;;; =>( − 1( − 2( − 3 0))) ;;; =>6 ;;; =>2

Dantam (Mines CSCI-534) Lisp Spring 2020 61 / 80 Common Lisp by Example Higher-order Functions

Application: MapReduce MapReduce: Simplified Data Processing on Large Clusters

User Program Function MapReduce(f,g,X) (1) fork (1) fork (1) fork 1 Y ← parallel-map(f , X ); Master (2) (2) 2 return reduce(g, Y ); assign assign map reduce

worker

split 0 (6) write output (5) remote worker file 0 split 1 (4) local write (5) read I Idea: (3) read split 2 worker I (parallel) map split 3 worker output I (serial) reduce/fold split 4 file 1 worker

I Provides scalability, Input Map Intermediate files Reduce Output fault-tolerance files phasr (on local disks) phase files Fig. 1. Execution overview.

I Implementations: Jeffrey7. When all map Deantasks and reduce and tasks have Sanjaybeen completed, the Ghemawat. mas- Handling Worker Failures ter wakes up the user program. At this point, the MapReduce call The master pings every worker periodically. If no response is received Google MapReduce in the user program returns back to the user code. from a worker in a certain amount of time, the master marks the worker I MapReduce: Simplified Dataas Processingfailed. Any map tasks completed on by the Large worker are reset back to their After successful completion, the output of the mapreduce execution initial idle state and therefore become eligible for scheduling on other I Apache Hadoop is available in the R output files (one per reduce task, with file names workers. Similarly, any map task or reduce task in progress on a failed Clustersspecified by the user).. Typically, Communications users do not need to combine these ofR worker the is also ACM. reset to idle and 2008. becomes eligible for rescheduling. output files into one file; they often pass these files as input to another Completed map tasks are reexecuted on a failure because their out- MapReduce call or use them from another distributed application that put is stored on the local disk(s) of the failed machine and is therefore is able to deal with input that is partitioned into multiple files. inaccessible. Completed reduce tasks do not need to be reexecuted Dantam (Mines CSCI-534) Lisp since their output is stored in a globalSpring file system. 2020 62 / 80 3.2 Master Data Structures When a map task is executed first by worker A and then later exe- The master keeps several data structures. For each map task and cuted by worker B (because A failed), all workers executing reduce reduce task, it stores the state (idle, in-progress, or completed) and the tasks are notified of the reexecution. Any reduce task that has not identity of the worker machine (for nonidle tasks). already read the data from worker A will read the data from worker B. The master is the conduit through which the location of interme- MapReduce is resilient to large-scale worker failures. For example, diate file regions is propagated from map tasks to reduce tasks. There- during one MapReduce operation, network maintenance on a running fore, for each completed map task, the master stores the locations and cluster was causing groups of 80 machines at a time to become unreach- sizes of the R intermediate file regions produced by the map task. able for several minutes. The MapReduce master simply reexecuted the Updates to this location and size information are received as map tasks work done by the unreachable worker machines and continued to make are completed. The information is pushed incrementally to workers forward progress, eventually completing the MapReduce operation. that have in-progress reduce tasks. Semantics in the Presence of Failures 3.3 Fault Tolerance When the user-supplied map and reduce operators are deterministic Since the MapReduce library is designed to help process very large functions of their input values, our distributed implementation pro- amounts of data using hundreds or thousands of machines, the library duces the same output as would have been produced by a nonfaulting must tolerate machine failures gracefully. sequential execution of the entire program.

COMMUNICATIONS OF THE ACM January 2008/Vol. 51, No. 1 109 Implementation Details Outline

Common Lisp by Example Basics Recursion First-class functions Higher-order Functions

Implementation Details

Programming Environment

Dantam (Mines CSCI-534) Lisp Spring 2020 63 / 80 Implementation Details Data Types

Definition (Data type) Example A classification of data/objects based on how I int the data/object is intended to or able to be use. I float List The set of values a variable may take. I I String I Structures: I int × string I float4 I Function: I int × int 7→ bool

Dantam (Mines CSCI-534) Lisp Spring 2020 64 / 80 Implementation Details Data Type Systems

I Type Checking/Binding Static: Check types at compile time (statically) Dynamic: Check types at run time (dynamically). I Type Enforcement Strong: Object types are strictly enforced Weak: Objects can be treated as different types (casting, “type punning”) I Examples: I C: static/weak I Python: dynamic/strong I ML and Haskell: static/strong I Lisp: dynamic (mostly) / strong

Dantam (Mines CSCI-534) Lisp Spring 2020 65 / 80 Implementation Details Machine Words – Representing Data word x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x 31 0 unsigned 42 0x2a 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 1 0 31 0 signed -42 0xffffffd6 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 0 1 1 0 31 0 float 5 42. = 1.3125 ∗ 2 0x42280000 1 0 0 0 0 1 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 31 0

Dantam (Mines CSCI-534) Lisp Spring 2020 66 / 80 Implementation Details Words and Types

word 1 1 0 0 0 0 0 0 0 1 0 0 1 0 0 1 0 0 0 0 1 1 1 1 1 1 0 1 0 0 0 0 31 0 ? 0xc0490fd0 −1068953648 (signed) ? 0xc0490fd0 3226013648 (unsigned) ? 0xc0490fd0 −3.141590 (float) ? 0xc0490fd0 valid pointer

Dantam (Mines CSCI-534) Lisp Spring 2020 67 / 80 Implementation Details Type Tags Data Tag

1 1 0 0 0 0 0 0 0 1 0 0 1 0 0 1 0 0 0 0 1 1 1 1 1 1 0 1 0 0 0 0 31 0

SBCL Tags (32-bit) data tag z }| { z }| { even fixnum Type Tag 0x180921FA × 000b (0x180921FA >> 2) Even Fixnum 000b 806503412 Odd Fixnum 100b Instance Pointer 001b List Pointer 011b Function Pointer 101b

Dantam (Mines CSCI-534) Lisp Spring 2020 68 / 80 Implementation Details Example: Tagged Storage

64 64 64 64 64 64 bit bit bit bit bit bit 64-bit SBCL: 1 1 NIL

Fixnum: (eq 1 1) t eq

1.0 1.0 NIL Single Float: (eq 1.0s0 1.0s0) t eq

Double Float: (eq 1.0d0 1.0d0) nil NIL

eq 1.0d0 1.0d0

Dantam (Mines CSCI-534) Lisp Spring 2020 69 / 80

Why? Implementation Details Example: SBCL Arrays

( l e t ( ( a ( make−array 5 : element−type ’double − f l o a t ) ) ) ;;.... )

64 64 64 64 64 data bit bit bit bit bit

0.0d0 0.0d0 0.0d0 0.0d0 0.0d0

A type descriptor

(SIMPLE-ARRAY DOUBLE-FLOAT (5))

Dantam (Mines CSCI-534) Lisp Spring 2020 70 / 80 Implementation Details Manual Memory Management

malloc(n) free(ptr) 1. Find a free block of at least n bytes 1. Add block back to the free list(s) 2. If no such block, get more memory from the OS 3. Return pointer to the block

Dantam (Mines CSCI-534) Lisp Spring 2020 71 / 80 Implementation Details Garbage Collection Roots Heap CPU registers Global Variables r0 h0 r1 h4 ... h1 h6 rk−1 rk h2 h7 h5 h3

Local Variables Stack

Dantam (Mines CSCI-534) Lisp Spring 2020 72 / 80 Programming Environment Outline

Common Lisp by Example Basics Recursion First-class functions Higher-order Functions

Implementation Details

Programming Environment

Dantam (Mines CSCI-534) Lisp Spring 2020 73 / 80 Programming Environment Lisp Programming Environment

I Lisp Editor, , Debugger available at runtime I Interactive development: Read-Eval-Print Loop (REPL)

C Programming Lisp Programming Lisp on unix source compile edit,compile,debug source code binary code editor lisp output edit debug output

Dantam (Mines CSCI-534) Lisp Spring 2020 74 / 80 Programming Environment SLIME Demo

source code

I SLIME, pstree I Read-Eval-Print-Loop (REPL) I DEFUN I DISASSEMBLE Emacs Steel Bank I Re-DEFUN Common Lisp output (SBCL)

Dantam (Mines CSCI-534) Lisp Spring 2020 75 / 80 Programming Environment SLIME Basics

I C: control I M: Meta / Alt I Frequently used: C-c C-k Compile and load file C-x C-e Evaluate expression before the point C-M-x Evaluate defun surround the point I Tab: auto-indent line/region I See SLIME drop-down in menu bar for more I https://common-lisp.net/project/slime/doc/html/

Dantam (Mines CSCI-534) Lisp Spring 2020 76 / 80 Programming Environment Style Notes

New Lines Closing Parenthesis Newlines emphasize structure: Closing parenthesis on same line:

Good Style Good Style ( and ( or a ( not b ) ) ( defun foo ( x ) ( or b c ) ) (+ 1 x ) )

Bad Style Bad Style ( and ( or a ( not b ) ) ( or b c ) ) ( defun foo ( x ) (+ 1 x ) )

Dantam (Mines CSCI-534) Lisp Spring 2020 77 / 80 Programming Environment Emacs Tips

Parenthesis Matching Viper Mode I vi implementation/emulator in Emacs I User Manual .emacs ( s e t q viper−mode t ) ( require ’ v i p e r )

.emacs ( show−paren−mode 1)

Dantam (Mines CSCI-534) Lisp Spring 2020 78 / 80 Programming Environment Why use Lisp? (Why learn something different?)

I Functional Programming: I Planning algorithms often are functional/recursive I Lisp has good support for functional programming. I Symbolic Computing: I Planning algorithms must often process symbolic expressions I Lisp has good support for symbolic processing I A good fit for (the first half of) this course

Dantam (Mines CSCI-534) Lisp Spring 2020 79 / 80 Programming Environment References

I Peter Seibel. . http://www.gigamonkeys.com/book/ I Common Lisp Hyperspec. http://www.lispworks.com/documentation/HyperSpec/Front/index.htm I . ANSI Common Lisp.

Dantam (Mines CSCI-534) Lisp Spring 2020 80 / 80