~rhcarvalho/+junk/racket

« back to all changes in this revision

Viewing changes to intro-to-racket/racket-lecture.rkt

  • Committer: Rodolfo Carvalho
  • Date: 2011-05-17 02:42:05 UTC
  • Revision ID: rhcarvalho@gmail.com-20110517024205-7p155j135ytm77h3
Intro do Racket as taken from the web

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#lang slideshow
 
2
 
 
3
(require slideshow/code)
 
4
(require scheme/pretty)
 
5
(require (file "compile.rkt"))
 
6
 
 
7
 
 
8
;; Library.
 
9
 
 
10
(define (large-text txt)
 
11
  (text txt (current-main-font) 62))
 
12
 
 
13
(define (pretty-syntax sexp . cols)
 
14
  (let ((port (open-input-string (apply pretty-format (cons sexp cols)))))
 
15
    (port-count-lines! port)
 
16
    (read-syntax "<no-name>" port)))
 
17
 
 
18
(define-syntax code-reduce
 
19
  (syntax-rules ()
 
20
    ((_ exp) 
 
21
     (let ((t exp))
 
22
       (hb-append (code exp) (tt " => ") (code (unsyntax t)))))))
 
23
 
 
24
(define comma ", ")
 
25
 
 
26
(define (fact n)
 
27
  (cond 
 
28
    ((<= n 0) 1)
 
29
    (else (* n (fact (- n 1))))))
 
30
     
 
31
 
 
32
 
 
33
;; Slides.
 
34
 
 
35
 
 
36
(slide
 
37
 (para #:align 'center 
 
38
       (large-text "Introduction to Racket"))
 
39
 (blank 50)
 
40
 (vc-append 0
 
41
            (t "Matthew Might")
 
42
            (colorize (bt "University of Utah") "red")
 
43
            (colorize (t "matt.might.net") "blue")))
 
44
 
 
45
 
 
46
(slide
 
47
 #:title "Why learn Racket (or Lisp)?"
 
48
 (para #:align 'center #:width 500
 
49
       "A good FORTRAN programmer can write FORTRAN in any language.")
 
50
 (blank 75)
 
51
 'next
 
52
 (para #:align 'center #:width 450 "A good Lisp programmer can write any language in Lisp.")
 
53
 )
 
54
 
 
55
 
 
56
(slide
 
57
 #:title "Why learn Racket (or Lisp)?"
 
58
 (para #:align 'center #:width 400
 
59
       "A good C programmer can write C in any language.")
 
60
 (blank 75)
 
61
 (para #:align 'center #:width 450 "A good Racket programmer can write any language in Racket.")
 
62
 )
 
63
 
 
64
 
 
65
 
 
66
(slide
 
67
 #:title "What is Racket?"
 
68
 
 
69
 (item "Lisp is λ-calculus done right.")
 
70
 (item "Scheme is Lisp done right.")
 
71
 (item "Racket is Scheme done right."))
 
72
 
 
73
 
 
74
 
 
75
(slide
 
76
 #:title "Origins of Scheme: Lisp"
 
77
 #:layout 'center
 
78
  (ht-append
 
79
   50
 
80
   (vc-append
 
81
    (scale (bitmap "jmcbw.jpg") 0.5)
 
82
    (t "John McCarthy"))
 
83
   (vl-append
 
84
    30   
 
85
    (item #:width 200 (t "Lisp created in 1958"))
 
86
    (item #:width 200 (t "Based on λ-calculus"))
 
87
    (item #:width 200 (t "Based on list-processing"))
 
88
    (item #:width 200 (t "S-Expression syntax"))
 
89
    (item #:width 200 (t "Code acts as data"))
 
90
    (item #:width 200 (t "Data acts as code"))
 
91
   )))
 
92
 
 
93
 
 
94
(slide
 
95
 #:title "Core S-Expression syntax"
 
96
 
 
97
 (tt "<s-exp> ::= <symbol>     ")
 
98
 (tt "         |  (<s-exp> ...)"))
 
99
 
 
100
 
 
101
(slide
 
102
 #:title "S-Expressions: Benefits"
 
103
 (vl-append
 
104
   30
 
105
   (blank 100)
 
106
   (item #:width 200 "Human-readable")
 
107
   (item #:width 200 "Human-writable")
 
108
   (item #:width 200 "Easy to parse")))
 
109
 
 
110
 
 
111
 
 
112
(slide
 
113
 #:title "S-Expression example"
 
114
 
 
115
 (vl-append
 
116
 0
 
117
 (tt "(ships                  ")
 
118
 (tt " (ship (name Enterprise)")
 
119
 (tt "       (x    120)       ")
 
120
 (tt "       (y    150))      ")
 
121
 (tt " (ship (name Galactica)   ")
 
122
 (tt "       (x    130)       ")
 
123
 (tt "       (y    42)))      ")))
 
124
 
 
125
(slide
 
126
 #:title "XML: High-calorie S-Expressions"
 
127
 (vl-append 
 
128
  0
 
129
  (tt "<ships>")
 
130
  
 
131
  (tt " <ship>") 
 
132
  (tt "  <name>Enterprise</name>")
 
133
  (tt "  <x>120</x>")
 
134
  (tt "  <y>150</y>")
 
135
  (tt " </ship>")
 
136
 
 
137
  (tt " <ship>") 
 
138
  (tt "  <name>Galactica</name>")
 
139
  (tt "  <x>130</x>")
 
140
  (tt "  <y>42</y>")
 
141
  (tt " </ship>")
 
142
  
 
143
  (tt "</ships>")
 
144
 ))
 
145
     
 
146
 
 
147
(slide
 
148
 #:title "S-Expressions: Universal syntax"
 
149
 (vl-append
 
150
  0
 
151
  (tt "class Dog extends Pet {")
 
152
  (tt " void bark() { ")
 
153
  (tt "  print(\"woof\") ; ")
 
154
  (tt "}}"))
 
155
 'next
 
156
 'alts
 
157
 (list
 
158
  (list (vl-append
 
159
         0
 
160
         (tt "(class Dog extends Pet")
 
161
         (tt " (void (bark) ")
 
162
         (tt "  (print \"woof\")))")))
 
163
        
 
164
  (list (vl-append
 
165
         0
 
166
         (tt "(class")
 
167
         (tt " (name    Dog)")
 
168
         (tt " (extends Pet)")
 
169
         (tt " (methods ")
 
170
         (tt "  (method ")
 
171
         (tt "   (name bark)")
 
172
         (tt "   (args ())")
 
173
         (tt "   (type (-> () void)) ")
 
174
         (tt "   (body (print \"woof\"))))))")))))
 
175
 
 
176
 
 
177
 
 
178
 
 
179
(slide
 
180
 #:title "Scheme"
 
181
 #:layout 'center
 
182
  (ht-append
 
183
   50
 
184
   (vc-append
 
185
    (bitmap "steele.jpg")
 
186
    (t "Guy Steele"))
 
187
   (vl-append
 
188
    20
 
189
    (item #:width 200 (t "Lisp done right, 1978"))
 
190
    (item #:width 200 (t "Fixed scoping oversight"))
 
191
    (item #:width 200 (t "Purified formal semantics"))
 
192
    (item #:width 200 (t "Hygienic macro system"))
 
193
   )))
 
194
 
 
195
 
 
196
 
 
197
 
 
198
(slide
 
199
 #:title "Racket"
 
200
 #:layout 'center
 
201
  (ht-append
 
202
   50
 
203
   (vc-append
 
204
    (bitmap "matthew.jpg")
 
205
    (t "Matthew Flatt"))
 
206
   (vl-append
 
207
    20
 
208
    (item #:width 200 (t "Scheme done right, 2010"))
 
209
    (item #:width 200 (t "Added huge library system"))
 
210
    (item #:width 200 (t "Adds modules, structs, etc."))
 
211
    (item #:width 200 (t "Batteries included"))
 
212
   )))
 
213
 
 
214
 
 
215
 
 
216
(slide
 
217
 #:title "Racket example: Factorial"
 
218
 (code
 
219
  (define (fact n)
 
220
    (cond
 
221
      ((<= n 0)  1)      
 
222
      (else      (* n (fact (- n 1))))))))
 
223
 
 
224
 
 
225
(slide
 
226
 #:title "Racket programs"
 
227
 (tt "<body> ::= <def> ... <exp> ..."))
 
228
 
 
229
(slide
 
230
 #:title "Definitions"
 
231
 (vl-append
 
232
  20
 
233
  (tt "<def> ::= (define (<id> <params>) <body>)")
 
234
  (tt "       |  (define <id> <exp>)")))
 
235
 
 
236
(define (id x) x)
 
237
 
 
238
(define (subset-scheme-slide show-core show-global show-basic show-sugar)
 
239
  (slide
 
240
   #:title "A subset of Scheme expressions"
 
241
   (vl-append
 
242
    (tt "<exp> ::= <var>")
 
243
    (show-basic (tt "       |  <num>"))
 
244
    (show-basic (tt "       |  <prim>"))
 
245
    (show-basic (tt "       |  <bool>"))
 
246
    (blank 15)
 
247
    (show-core (tt "       |  (<exp> <exp> ...)"))
 
248
    (show-core (tt "       |  (λ (<params>) <body>)"))
 
249
    (blank 15)  
 
250
    (show-basic (tt "       |  (if <exp> <exp> <exp>)"))
 
251
    (show-sugar (tt "       |  (cond (<exp> <exp>) ...)"))
 
252
    (blank 15)
 
253
    (show-sugar (tt "       |  (let ((<var> <exp>)  ...) <body>)"))
 
254
    (show-sugar (tt "       |  (let* ((<var> <exp>) ...) <body>)"))
 
255
    (show-sugar (tt "       |  (letrec ((<var> <exp>) ...) <body>)"))
 
256
    (blank 15)
 
257
    (show-sugar (tt "       |  (quote <sexp>)"))
 
258
    (show-sugar (tt "       |  (quasiquote <qq-sexp>)"))
 
259
    (blank 15)
 
260
    (show-global (tt "       |  (begin <body>)"))
 
261
    (show-global (tt "       |  (set! <var> <exp>)"))
 
262
    )))
 
263
 
 
264
;(subset-scheme-slide id id id id)
 
265
;(subset-scheme-slide id id id ghost)
 
266
;(subset-scheme-slide id id ghost ghost)
 
267
;(subset-scheme-slide id ghost ghost ghost)
 
268
 
 
269
 
 
270
 
 
271
 
 
272
(slide 
 
273
 (large-text "A tour of Racket"))
 
274
 
 
275
 
 
276
 
 
277
(slide
 
278
 #:title "Numbers"
 
279
 (item "Integers: " (code 1) "," (code -3))
 
280
 
 
281
 (item "Rationals: " (code-reduce (/ 8 10)))
 
282
 
 
283
 (item "\"Reals\": " (code-reduce (sqrt 2)))
 
284
 
 
285
 (item "Imaginary: " (code-reduce (sqrt -1)))
 
286
 
 
287
 (item "Complex: " (code-reduce (* 1+3i 1-22/7i)))
 
288
 
 
289
 (item "Precise: " (code-reduce (fact 18))))
 
290
 
 
291
(slide
 
292
 #:title "Booleans"
 
293
 (item "Boolean literals " (code #t) ", " (code #f))
 
294
 (item "Only " (code #f) " is false")
 
295
 (item "Anything else is true")
 
296
 )
 
297
 
 
298
 
 
299
 
 
300
 
 
301
(slide
 
302
 #:title "Lists"
 
303
 (item (code cons) ", " (code car) ", " (code cdr) ", " (code '())
 
304
       ", " (code pair?))
 
305
 'next
 
306
 (item (code-reduce (cons 1 '())))
 
307
 'next
 
308
 (item (code-reduce (cons 3 (cons 1 '()))))
 
309
 'next
 
310
 (item (code-reduce '(3 1)))
 
311
 'next
 
312
 (item (code-reduce (car (cons 3 (cons 1 '())))))
 
313
 'next
 
314
 (item (code-reduce (cdr (cons 3 (cons 1 '())))))
 
315
 'next
 
316
 (item (code-reduce (pair? (cons 3 4))))
 
317
 'next
 
318
 (item (code-reduce (null? '())))
 
319
 )
 
320
 
 
321
 
 
322
(slide
 
323
 #:title "Quoted S-Expressions"
 
324
 (item (code '<s-exp>) (tt " == ") (tt "(quote <s-exp>)"))
 
325
 (item (code-reduce '<id>))
 
326
 
 
327
 (item (code-reduce '3))
 
328
 (item (code-reduce 'a))
 
329
 (item (code-reduce '(1 2 3)))
 
330
 (item (code-reduce (cdr '(1 2 3))))
 
331
 (item (code-reduce '(f (g x))))
 
332
 (item (code-reduce (car '(f (g x)))))
 
333
 
 
334
 (item (code-reduce '())))
 
335
 
 
336
 
 
337
 
 
338
(slide
 
339
 #:title "Primitives"
 
340
 (item "Arithmetic: " (code +) ", " (code modulo) ", " (code quotient))
 
341
 (item "Types: " (code null?) ", " (code pair?) ", " (code number?) ", " (code integer?))
 
342
 (item "Lists: " (code list) comma (code cons) ", " (code car)", " (code cdr))
 
343
 (item "Equality: " (code =)", " (code eq?)", " (code eqv?) comma (code equal?))
 
344
 (item "Ex:" (code-reduce (gcd 248 184)))
 
345
 (item "Ex:" (code-reduce (cons 3 (cons 4 '()))))
 
346
 )
 
347
 
 
348
 
 
349
 
 
350
 
 
351
 
 
352
 
 
353
(slide 
 
354
 #:title (code let)
 
355
 
 
356
 (item (para #:width 200 #:align 'left 
 
357
             (tt "<let-exp> ::= ") 
 
358
             (tt " (let ((<var> <exp>) ...) ")
 
359
             (tt "  <body>)")))
 
360
 (item (code let) " binds values to variable names")
 
361
 'next
 
362
 (item (code-reduce (let ((π   3.14)
 
363
                          (two 2))
 
364
                      (* two π))))
 
365
 'next
 
366
 (item (code (let ((π   3.14)
 
367
                   (2*π (* 2 π)))
 
368
               2*π)) (colorize (tt " =/=> ") "red") (code 6.28)))
 
369
 
 
370
 
 
371
 
 
372
 
 
373
(slide 
 
374
 #:title (code let*)
 
375
 
 
376
 (item (para #:width 200 #:align 'left 
 
377
             (tt "<let*-exp> ::= ") 
 
378
             (tt " (let* ((<var> <exp>) ...) ")
 
379
             (tt "  <body>)")))
 
380
 (item (code let*) " binds values to variable names sequentially")
 
381
 'next
 
382
 (item (code-reduce (let* ((π   3.14)
 
383
                           (π   3.14159))
 
384
                      (* 2 π))))
 
385
 'next
 
386
 (item (code-reduce (let* ((π   3.14)
 
387
                           (2*π (* 2 π)))
 
388
                      2*π))))
 
389
 
 
390
 
 
391
 
 
392
 
 
393
(slide
 
394
 #:title "Variables"
 
395
 
 
396
 (item "Any symbol allowed")
 
397
 
 
398
 (item "Roughly: " (tt "[^()'`,]+"))
 
399
 
 
400
 (item "Ex:" (code foo))
 
401
 
 
402
 (item "Ex:" (code foo-bar))
 
403
 
 
404
 (item "Ex:" (code π) comma (code Δ) comma (code ∞) comma (code Σ) comma (code ⊑) comma (code ∅) comma (code →))
 
405
 
 
406
 (item "Ex:" (code pair?))
 
407
 
 
408
 (item "Ex:" (code call-with-current-continuation))
 
409
 
 
410
 (item "Ex:" (code-reduce (let ((let 3)) let))))
 
411
 
 
412
 
 
413
 
 
414
 
 
415
(slide
 
416
 #:title "λ"
 
417
 (item "λ produces anonymous functions")
 
418
 (item (tt "<lambda> ::= (λ (<var> ...) <body>)"))
 
419
; (item (tt "<params> ::= <var> ..."))
 
420
 (item (code-reduce ((λ (x) (* x x)) 3)))
 
421
 (item (code (let ((λ (λ (λ) (λ λ)))) (λ λ)))))
 
422
 
 
423
 
 
424
 
 
425
 
 
426
(slide
 
427
 #:title "Lexical scope"
 
428
 
 
429
 (t "To what does the following evaluate?")
 
430
 
 
431
 (blank)
 
432
 
 
433
 (code
 
434
  (let ((x 20))
 
435
    (let* ((x 3)
 
436
           (f (λ () x))
 
437
           (x 10))
 
438
      (f)))))
 
439
 
 
440
 
 
441
 
 
442
(slide
 
443
 #:title "Lexical scope"
 
444
 (item "λ-terms capture bindings in scope")
 
445
 (item "WYSIWYG for anonymous functions"))
 
446
 
 
447
 
 
448
 
 
449
 
 
450
 
 
451
(slide
 
452
 #:title "Obligatory derivative example"
 
453
 
 
454
 (code
 
455
  (define ε 0.001)
 
456
  
 
457
  (define (D f) (λ (x) (/ (- (f (+ x ε)) (f x))
 
458
                          ε)))
 
459
  
 
460
  (define (g x) (* x x))
 
461
  
 
462
  ((D g) 0) ≈ 0
 
463
 
 
464
  ((D g) 2) ≈ 4))
 
465
 
 
466
 
 
467
 
 
468
 
 
469
(slide
 
470
 #:title "Conditionals"
 
471
 (item (tt "<if-exp> ::= (if <exp> <exp> <exp>)"))
 
472
 (item (tt "<cond-exp> ::= ") (tt " (cond (<exp> <exp>) ...)"))
 
473
 (item (code-reduce (if 'a 'b 'c)))
 
474
 (item (code-reduce (if #f 'b 'c)))
 
475
 (item (code-reduce (cond
 
476
                      ((< 3 0) 'negative)
 
477
                      ((= 3 0) 'zero)
 
478
                      ((> 3 0) 'positive)))))
 
479
 
 
480
 
 
481
 
 
482
(slide
 
483
 #:title "Quasiquotes"
 
484
 (item "Special syntax for constructing literals")
 
485
 (item (tt "`<qq-sexp>") (tt " == ") (tt "(quasiquote <qq-sexp>)"))
 
486
 (item (tt ",<exp>") (tt " == ") (tt "(unquote <exp>)"))
 
487
 (item (code-reduce `(3 + 4 is ,(+ 3 4))))
 
488
 (code
 
489
  (let ((new-ship (λ (name x y)
 
490
                    `(ship (name ,name)
 
491
                           (x    ,x)
 
492
                           (y    ,y)))))
 
493
    (new-ship 'Enterprise 42 1701)))
 
494
 'next
 
495
 (code => (ship (name Enteprise) 
 
496
                (x 42) 
 
497
                (y 1701))))
 
498
  
 
499
 
 
500
(slide
 
501
 #:title "Mutating variables"
 
502
 
 
503
 (item (code begin) " sequences expressions, like " (tt "{}")  " in C")
 
504
 
 
505
 (code-reduce
 
506
  (begin
 
507
    1
 
508
    2
 
509
    3))
 
510
 
 
511
 (item (code set!) " assigns to a variable, like " (tt "=") " in C")
 
512
 
 
513
 (code-reduce
 
514
  (let ((π 3.14))
 
515
    (begin
 
516
      (set! π 3.14159)
 
517
      π)))
 
518
 
 
519
 )
 
520
 
 
521
 
 
522
(slide 
 
523
 #:title "Pattern-matching"
 
524
 (item "Powerful tool for dissecting data like trees")
 
525
 'next
 
526
  (code-reduce
 
527
   (match '(1 2 3)
 
528
     [`(1 ,x 4)  x]
 
529
     [`(1 2 ,z)  z]))
 
530
  'next
 
531
  (code-reduce
 
532
   (match '(1 3 5 9)
 
533
     [(list 1 (? even?) ...)
 
534
      'all-even]
 
535
     [(list 1 (? odd?) ...)
 
536
      'all-odd])))