#lang slideshow (require slideshow/code) (require scheme/pretty) (require (file "compile.rkt")) ;; Library. (define (large-text txt) (text txt (current-main-font) 62)) (define (pretty-syntax sexp . cols) (let ((port (open-input-string (apply pretty-format (cons sexp cols))))) (port-count-lines! port) (read-syntax "" port))) (define-syntax code-reduce (syntax-rules () ((_ exp) (let ((t exp)) (hb-append (code exp) (tt " => ") (code (unsyntax t))))))) (define comma ", ") (define (fact n) (cond ((<= n 0) 1) (else (* n (fact (- n 1)))))) ;; Slides. (slide (para #:align 'center (large-text "Introduction to Racket")) (blank 50) (vc-append 0 (t "Matthew Might") (colorize (bt "University of Utah") "red") (colorize (t "matt.might.net") "blue"))) (slide #:title "Why learn Racket (or Lisp)?" (para #:align 'center #:width 500 "A good FORTRAN programmer can write FORTRAN in any language.") (blank 75) 'next (para #:align 'center #:width 450 "A good Lisp programmer can write any language in Lisp.") ) (slide #:title "Why learn Racket (or Lisp)?" (para #:align 'center #:width 400 "A good C programmer can write C in any language.") (blank 75) (para #:align 'center #:width 450 "A good Racket programmer can write any language in Racket.") ) (slide #:title "What is Racket?" (item "Lisp is λ-calculus done right.") (item "Scheme is Lisp done right.") (item "Racket is Scheme done right.")) (slide #:title "Origins of Scheme: Lisp" #:layout 'center (ht-append 50 (vc-append (scale (bitmap "jmcbw.jpg") 0.5) (t "John McCarthy")) (vl-append 30 (item #:width 200 (t "Lisp created in 1958")) (item #:width 200 (t "Based on λ-calculus")) (item #:width 200 (t "Based on list-processing")) (item #:width 200 (t "S-Expression syntax")) (item #:width 200 (t "Code acts as data")) (item #:width 200 (t "Data acts as code")) ))) (slide #:title "Core S-Expression syntax" (tt " ::= ") (tt " | ( ...)")) (slide #:title "S-Expressions: Benefits" (vl-append 30 (blank 100) (item #:width 200 "Human-readable") (item #:width 200 "Human-writable") (item #:width 200 "Easy to parse"))) (slide #:title "S-Expression example" (vl-append 0 (tt "(ships ") (tt " (ship (name Enterprise)") (tt " (x 120) ") (tt " (y 150)) ") (tt " (ship (name Galactica) ") (tt " (x 130) ") (tt " (y 42))) "))) (slide #:title "XML: High-calorie S-Expressions" (vl-append 0 (tt "") (tt " ") (tt " Enterprise") (tt " 120") (tt " 150") (tt " ") (tt " ") (tt " Galactica") (tt " 130") (tt " 42") (tt " ") (tt "") )) (slide #:title "S-Expressions: Universal syntax" (vl-append 0 (tt "class Dog extends Pet {") (tt " void bark() { ") (tt " print(\"woof\") ; ") (tt "}}")) 'next 'alts (list (list (vl-append 0 (tt "(class Dog extends Pet") (tt " (void (bark) ") (tt " (print \"woof\")))"))) (list (vl-append 0 (tt "(class") (tt " (name Dog)") (tt " (extends Pet)") (tt " (methods ") (tt " (method ") (tt " (name bark)") (tt " (args ())") (tt " (type (-> () void)) ") (tt " (body (print \"woof\"))))))"))))) (slide #:title "Scheme" #:layout 'center (ht-append 50 (vc-append (bitmap "steele.jpg") (t "Guy Steele")) (vl-append 20 (item #:width 200 (t "Lisp done right, 1978")) (item #:width 200 (t "Fixed scoping oversight")) (item #:width 200 (t "Purified formal semantics")) (item #:width 200 (t "Hygienic macro system")) ))) (slide #:title "Racket" #:layout 'center (ht-append 50 (vc-append (bitmap "matthew.jpg") (t "Matthew Flatt")) (vl-append 20 (item #:width 200 (t "Scheme done right, 2010")) (item #:width 200 (t "Added huge library system")) (item #:width 200 (t "Adds modules, structs, etc.")) (item #:width 200 (t "Batteries included")) ))) (slide #:title "Racket example: Factorial" (code (define (fact n) (cond ((<= n 0) 1) (else (* n (fact (- n 1)))))))) (slide #:title "Racket programs" (tt " ::= ... ...")) (slide #:title "Definitions" (vl-append 20 (tt " ::= (define ( ) )") (tt " | (define )"))) (define (id x) x) (define (subset-scheme-slide show-core show-global show-basic show-sugar) (slide #:title "A subset of Scheme expressions" (vl-append (tt " ::= ") (show-basic (tt " | ")) (show-basic (tt " | ")) (show-basic (tt " | ")) (blank 15) (show-core (tt " | ( ...)")) (show-core (tt " | (λ () )")) (blank 15) (show-basic (tt " | (if )")) (show-sugar (tt " | (cond ( ) ...)")) (blank 15) (show-sugar (tt " | (let (( ) ...) )")) (show-sugar (tt " | (let* (( ) ...) )")) (show-sugar (tt " | (letrec (( ) ...) )")) (blank 15) (show-sugar (tt " | (quote )")) (show-sugar (tt " | (quasiquote )")) (blank 15) (show-global (tt " | (begin )")) (show-global (tt " | (set! )")) ))) ;(subset-scheme-slide id id id id) ;(subset-scheme-slide id id id ghost) ;(subset-scheme-slide id id ghost ghost) ;(subset-scheme-slide id ghost ghost ghost) (slide (large-text "A tour of Racket")) (slide #:title "Numbers" (item "Integers: " (code 1) "," (code -3)) (item "Rationals: " (code-reduce (/ 8 10))) (item "\"Reals\": " (code-reduce (sqrt 2))) (item "Imaginary: " (code-reduce (sqrt -1))) (item "Complex: " (code-reduce (* 1+3i 1-22/7i))) (item "Precise: " (code-reduce (fact 18)))) (slide #:title "Booleans" (item "Boolean literals " (code #t) ", " (code #f)) (item "Only " (code #f) " is false") (item "Anything else is true") ) (slide #:title "Lists" (item (code cons) ", " (code car) ", " (code cdr) ", " (code '()) ", " (code pair?)) 'next (item (code-reduce (cons 1 '()))) 'next (item (code-reduce (cons 3 (cons 1 '())))) 'next (item (code-reduce '(3 1))) 'next (item (code-reduce (car (cons 3 (cons 1 '()))))) 'next (item (code-reduce (cdr (cons 3 (cons 1 '()))))) 'next (item (code-reduce (pair? (cons 3 4)))) 'next (item (code-reduce (null? '()))) ) (slide #:title "Quoted S-Expressions" (item (code ') (tt " == ") (tt "(quote )")) (item (code-reduce ')) (item (code-reduce '3)) (item (code-reduce 'a)) (item (code-reduce '(1 2 3))) (item (code-reduce (cdr '(1 2 3)))) (item (code-reduce '(f (g x)))) (item (code-reduce (car '(f (g x))))) (item (code-reduce '()))) (slide #:title "Primitives" (item "Arithmetic: " (code +) ", " (code modulo) ", " (code quotient)) (item "Types: " (code null?) ", " (code pair?) ", " (code number?) ", " (code integer?)) (item "Lists: " (code list) comma (code cons) ", " (code car)", " (code cdr)) (item "Equality: " (code =)", " (code eq?)", " (code eqv?) comma (code equal?)) (item "Ex:" (code-reduce (gcd 248 184))) (item "Ex:" (code-reduce (cons 3 (cons 4 '())))) ) (slide #:title (code let) (item (para #:width 200 #:align 'left (tt " ::= ") (tt " (let (( ) ...) ") (tt " )"))) (item (code let) " binds values to variable names") 'next (item (code-reduce (let ((π 3.14) (two 2)) (* two π)))) 'next (item (code (let ((π 3.14) (2*π (* 2 π))) 2*π)) (colorize (tt " =/=> ") "red") (code 6.28))) (slide #:title (code let*) (item (para #:width 200 #:align 'left (tt " ::= ") (tt " (let* (( ) ...) ") (tt " )"))) (item (code let*) " binds values to variable names sequentially") 'next (item (code-reduce (let* ((π 3.14) (π 3.14159)) (* 2 π)))) 'next (item (code-reduce (let* ((π 3.14) (2*π (* 2 π))) 2*π)))) (slide #:title "Variables" (item "Any symbol allowed") (item "Roughly: " (tt "[^()'`,]+")) (item "Ex:" (code foo)) (item "Ex:" (code foo-bar)) (item "Ex:" (code π) comma (code Δ) comma (code ∞) comma (code Σ) comma (code ⊑) comma (code ∅) comma (code →)) (item "Ex:" (code pair?)) (item "Ex:" (code call-with-current-continuation)) (item "Ex:" (code-reduce (let ((let 3)) let)))) (slide #:title "λ" (item "λ produces anonymous functions") (item (tt " ::= (λ ( ...) )")) ; (item (tt " ::= ...")) (item (code-reduce ((λ (x) (* x x)) 3))) (item (code (let ((λ (λ (λ) (λ λ)))) (λ λ))))) (slide #:title "Lexical scope" (t "To what does the following evaluate?") (blank) (code (let ((x 20)) (let* ((x 3) (f (λ () x)) (x 10)) (f))))) (slide #:title "Lexical scope" (item "λ-terms capture bindings in scope") (item "WYSIWYG for anonymous functions")) (slide #:title "Obligatory derivative example" (code (define ε 0.001) (define (D f) (λ (x) (/ (- (f (+ x ε)) (f x)) ε))) (define (g x) (* x x)) ((D g) 0) ≈ 0 ((D g) 2) ≈ 4)) (slide #:title "Conditionals" (item (tt " ::= (if )")) (item (tt " ::= ") (tt " (cond ( ) ...)")) (item (code-reduce (if 'a 'b 'c))) (item (code-reduce (if #f 'b 'c))) (item (code-reduce (cond ((< 3 0) 'negative) ((= 3 0) 'zero) ((> 3 0) 'positive))))) (slide #:title "Quasiquotes" (item "Special syntax for constructing literals") (item (tt "`") (tt " == ") (tt "(quasiquote )")) (item (tt ",") (tt " == ") (tt "(unquote )")) (item (code-reduce `(3 + 4 is ,(+ 3 4)))) (code (let ((new-ship (λ (name x y) `(ship (name ,name) (x ,x) (y ,y))))) (new-ship 'Enterprise 42 1701))) 'next (code => (ship (name Enteprise) (x 42) (y 1701)))) (slide #:title "Mutating variables" (item (code begin) " sequences expressions, like " (tt "{}") " in C") (code-reduce (begin 1 2 3)) (item (code set!) " assigns to a variable, like " (tt "=") " in C") (code-reduce (let ((π 3.14)) (begin (set! π 3.14159) π))) ) (slide #:title "Pattern-matching" (item "Powerful tool for dissecting data like trees") 'next (code-reduce (match '(1 2 3) [`(1 ,x 4) x] [`(1 2 ,z) z])) 'next (code-reduce (match '(1 3 5 9) [(list 1 (? even?) ...) 'all-even] [(list 1 (? odd?) ...) 'all-odd])))