What is it?
• Programming languages should be designed not by piling feature on top of feature, but by removing the weaknesses and restrictions that make additional features appear necessary.
- r6rs
1
Language
• Normal form
2
Language
• Normal form (+ 1 9 4)
3
Language
• Normal form (+ 1 9 4) (display (format "id~s~d~n" str int-value))
4
Language
• Normal form (+ 1 9 4) (display (format "id~s~d~n" str int-value)) • Special forms
5
Language
• Normal form (+ 1 9 4) (display (format "id~s~d~n" str int-value)) • Special forms (define (fib n) (case n ((0) 0) ((1) 1) (else (+ (fib (- n 2)) (fib (- n 1))))))
6
Language
• Normal form (+ 1 9 4) (display (format "id~s~d~n" str int-value)) • Special forms (define (fib n) (case n ((0) 0) ((1) 1) (else (+ (fib (- n 2)) (fib (- n 1)))))) (let ((x 0) (y 1)) (+ x y))
7
Format
(define (fib n) (case n ((0) 0) ((1) 1) (else (+ (fib (- n 2)) (fib (- n 1)))))) (define fib (lambda (n) (case n [(0) 0] [(1) 1] [else (+ (fib (- n 2)) (fib (- n 1)))])))
8
What is it?
• Lisp • Dynamically typed • Dynamically dispatched • Lexically scoped • Garbage collected • Hygienic macros • Continuations • Live language
9
Lisp - Function
(define (fact x) (if (zero? x) 1 (* x (fact (- x 1)))))
10
Lisp - Function
(define (fact x) (if (zero? x) 1 (* x (fact (- x 1))))) (fact 5) (* 5 (fact 4)) (* 5 (* 4 (fact 3))) (* 5 (* 4 (* 3 (fact 2)))) (* 5 (* 4 (* 3 (* 2 (fact 1))))) (* 5 (* 4 (* 3 (* 2 (* 1 (fact 0)))))) (* 5 (* 4 (* 3 (* 2 (* 1 1))))) (* 5 (* 4 (* 3 (* 2 1)))) (* 5 (* 4 (* 3 2))) (* 5 (* 4 6)) (* 5 24) 120
11
Lisp - imperative
(define (fact x) (define (iter n accum) (if (zero? n) accum (iter (- n 1) (* n accum)))) (iter x 1))
12
Lisp - imperative
(define (fact x) (define (iter n accum) (if (zero? n) accum (iter (- n 1) (* n accum)))) (iter x 1)) (fact (iter (iter (iter (iter (iter (iter 120 5) 5 1) 4 5) 3 20) 2 60) 1 120) 0 120)
13
Dynamically typed
• Variables are typed on contents (let ((x 'tag) (y 10) (z "0.9")) (set! x 1234.5))
14
Dynamically dispatched
• Single global name space (define x 10) (set! x (lambda (y) (+ 10 y)))
15
Lexically scoped
(define y 10) (set! x (lambda (x) (+ 10 y x)))
16
Garbage collected
(define big-alloc (lambda (n) (if (<= n 0) '() (cons n (big-alloc (- n 1))))) (big-alloc 1000000)
17
Hygenic macros
(with-matrix (look-at 10 10 20 0 0 5 0 1 0) (with-matrix (translate 10 10 10) (draw-object)))
18
Hygenic macros
• Construct code at compile time using scheme • Create new special forms
(define-syntax with-matrix (syntax-rules (translate rotate scale identity) ((_ (translate x y z) body ...) (begin (gl-push-matrix) (gl-translate x y z) body ... (gl-pop-matrix))) ((_ (rotate theta x y z) body ...) (begin (gl-push-matrix) (gl-rotate theta x y z) body ... (gl-pop-matrix))) ((_ (scale x y z) body ...) (begin (gl-push-matrix) (gl-scale x y z) body ... (gl-pop-matrix))) ((_ (look-at eyex eyey eyez centerx centery centerz upx upy upz) body ...) (begin (gl-push-matrix) (gl-mult-matrix (lookat-matrix eyex eyey eyez centerx centery centerz upx upy upz)) (gl-translate (- eyex) (- eyey) (- eyez)) body ... (gl-pop-matrix)))))
19
Hygenic macros
• Not too much different from simple CPP #define WITH_TRANSFORM(x, y, z, do) \ {glPushMatrix(); \ glTranslate(x,y,z); \ {do} \ glPop(); }
20
Hygenic macros
• Not too much different from simple CPP #define WITH_TRANSFORM(x, y, z, do) \ {glPushMatrix(); \ glTranslate(x,y,z); \ {do} \ glPop(); } • But it doesn’t end there
21
Hygenic macros
• Imagine you had the whole power of C++ at compile time • Any program you could run, could be a CPP define
22
Hygenic macros
• Imagine you had the whole power of C++ at compile time • Any program you could run, could be a CPP define int addThem(int a, int b) { return a + b; } #define SUM(a,b) `,addThem(a,b);
23
Hygenic macros
• More than just string replacement • Compile time code (define (machines-from-sql) '("ps3_dgiberson" "ps3_ttest1")) (define-syntax machines (syntax-rules () ((_) `( ,@(machines-from-sql)))))
24
Hygenic macros
• But it continues (with-matrix (look-at 10 10 20 0 0 5 0 1 0) (with-matrix (translate 10 10 10) (draw-object)))
25
Hygenic macros
• But it continues (with-matrix (look-at 10 10 20 0 0 5 0 1 0) (with-matrix (translate 10 10 10) (draw-object))) • Because you can create new control flows, you can also do optimizations (with-matrix m11 m12 m13 m14 m21 m22 m23 m24 m31 m32 m33 m34 m41 m42 m43 m44 (draw-object))
26
Continuations
• Grab the state at and point and get a handle to go back there (define (search wanted lst) (call/cc (lambda (return) (for-each (lambda (element) (if (wanted? element) (return element))) lst) #f)))
27
Live code
• Simple example • OpenGL rendering
28