(Scheme)

1,280 views

Published on

What's amazing about Scheme is that it's small and easy to learn, but still amazingly powerful. Scheme's code-is-data philosophy allows you to invent your own language features. And it's functional.

We'll start with an overview of the language and how code is represented, tinker with some self-modifying code, invent a few of our own language constructs, then show off a few brain-warping control structures.

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,280
On SlideShare
0
From Embeds
0
Number of Embeds
828
Actions
Shares
0
Downloads
12
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

(Scheme)

  1. 1. (Scheme) Jason Felice @eraserhd http://objvimmer.com/Friday, January 25, 13
  2. 2. The Syntax (* 7 9)Friday, January 25, 13
  3. 3. The Syntax (* 7 9) • Read an expressionFriday, January 25, 13
  4. 4. The Syntax (* 7 9) • Read an expression • Evaluate the elementsFriday, January 25, 13
  5. 5. The Syntax ( λ 7 9) • Read an expression • Evaluate the elementsFriday, January 25, 13
  6. 6. The Syntax ( λ 7 9) • Read an expression • Evaluate the elementsFriday, January 25, 13
  7. 7. The Syntax ( λ 7 9) • Read an expression • Evaluate the elementsFriday, January 25, 13
  8. 8. The Syntax 63 • Read an expression • Evaluate the elements • Invoke the function, replace with resultFriday, January 25, 13
  9. 9. The Syntax (* 7 (+ 10 1)) • Read an expressionFriday, January 25, 13
  10. 10. The Syntax (* 7 (+ 10 1)) • Read an expression • Evaluate the elementsFriday, January 25, 13
  11. 11. The Syntax ( λ 7 (+ 10 1)) • Read an expression • Evaluate the elementsFriday, January 25, 13
  12. 12. The Syntax ( λ 7 (+ 10 1)) • Read an expression • Evaluate the elementsFriday, January 25, 13
  13. 13. The Syntax ( λ 7 11) • Read an expression • Evaluate the elementsFriday, January 25, 13
  14. 14. The Syntax 77 • Read an expression • Evaluate the elements • Invoke the function, replace with resultFriday, January 25, 13
  15. 15. The REPL • Evaluates code interactivelyFriday, January 25, 13
  16. 16. The REPL • Evaluates code interactively • Prints the result (if its specified)Friday, January 25, 13
  17. 17. Hello, World! (display "Hello, World!") (newline)Friday, January 25, 13
  18. 18. Hello, World! (define (say-hello) (display "Hello, World!") (newline)) (say-hello)Friday, January 25, 13
  19. 19. Hello, World! (define (say-hello) (display "Hello, World!") (newline)) (say-hello)Friday, January 25, 13
  20. 20. Hello, World! (define (say-hello) (display "Hello, World!") (newline)) (say-hello)Friday, January 25, 13
  21. 21. factorial 5! = 5 * 4 * 3 * 2 * 1 n! = n * (n - 1) * (n - 2) * ... * 1Friday, January 25, 13
  22. 22. factorial n! = { 1, n=1 n(n-1)!, n>1 1! = 1 5! = 5•4!Friday, January 25, 13
  23. 23. factorial (define (factorial n) (if (= n 1) 1 (* n (factorial (- n 1))))Friday, January 25, 13
  24. 24. factorial (define (factorial n) (if (= n 1) 1 (* n (factorial (- n 1))))Friday, January 25, 13
  25. 25. factorial (define (factorial n) (if (= n 1) 1 (* n (factorial (- n 1))))Friday, January 25, 13
  26. 26. factorial (define (factorial n) (if (= n 1) 1 (* n (factorial (- n 1))))Friday, January 25, 13
  27. 27. factorial (define (factorial n) (if (= n 1) 1 (* n (factorial (- n 1))))Friday, January 25, 13
  28. 28. Lists (1 2 3)Friday, January 25, 13
  29. 29. Lists (1 2 3) 1 2 3 ()Friday, January 25, 13
  30. 30. Pairs (1 . 2) 1 2Friday, January 25, 13
  31. 31. Pairs (1 . 2) car 1 2Friday, January 25, 13
  32. 32. Pairs (1 . 2) car 1 2 cdrFriday, January 25, 13
  33. 33. Friday, January 25, 13
  34. 34. Lists (1 . (2 . (3 . ()))) 1 2 3 ()Friday, January 25, 13
  35. 35. Lists (1 . (2 . (3 . ()))) 1 2 3 ()Friday, January 25, 13
  36. 36. Lists (1 . (2 . (3 . ()))) 1 2 3 ()Friday, January 25, 13
  37. 37. Lists (1 . (2 . (3 . ()))) 1 2 3 ()Friday, January 25, 13
  38. 38. Code is Data (sqrt (+ (* x x) (* y y)))Friday, January 25, 13
  39. 39. Code is Data (sqrt (+ (* x x) (* y y))) * x x * y y () () + () sqrt ()Friday, January 25, 13
  40. 40. Code is Data (* 7 9) 63Friday, January 25, 13
  41. 41. Code is Data (* 7 9) 63 (* 7 9) (* 7 9)Friday, January 25, 13
  42. 42. Code is Data (* 7 9) 63 (* 7 9) (* 7 9) (quote (* 7 9)) (* 7 9)Friday, January 25, 13
  43. 43. Code is Data (* 7 9) 63 (* 7 9) (* 7 9) (quote (* 7 9)) (* 7 9) (eval (* 7 9)) 63Friday, January 25, 13
  44. 44. Pwoblem! (quote (* 7 9)) (* 7 9)Friday, January 25, 13
  45. 45. Pwoblem! (quote (* 7 9)) • Read an expression • Evaluate the elements • Invoke the function, replace with resultFriday, January 25, 13
  46. 46. Pwoblem! ( λ (* 7 9)) • Read an expression • Evaluate the elements • Invoke the function, replace with resultFriday, January 25, 13
  47. 47. Pwoblem! ( λ 63) • Read an expression • Evaluate the elements • Invoke the function, replace with resultFriday, January 25, 13
  48. 48. Pwoblem! ( λ 63) (* 7 9) • Read an expression • Evaluate the elements • Invoke the function, replace with resultFriday, January 25, 13
  49. 49. Macros • Macros transform expressions • Given unevaluated arguments • Return code which replaces the macroFriday, January 25, 13
  50. 50. Macros (quote (* 7 9)) • Read an expression • Evaluate the elements • Invoke the function, replace with resultFriday, January 25, 13
  51. 51. Macros ( λ (* 7 9)) • Read an expression • Evaluate the elements • Invoke the function, replace with resultFriday, January 25, 13
  52. 52. Macros ( λ (* 7 9)) • Read an expression • Evaluate the elements, but not for macros • Invoke the function, replace with resultFriday, January 25, 13
  53. 53. Macros ?? (* 7 9) • Read an expression • Evaluate the elements, but not for macros • Invoke the macro, and evaluate its resultFriday, January 25, 13
  54. 54. Macros (define-syntax unless (syntax-rules (unless) ((unless condition then) (if (not condition) then))))Friday, January 25, 13
  55. 55. Macros (define-syntax unless (syntax-rules (unless) ((unless condition then) (if (not condition) then))))Friday, January 25, 13
  56. 56. Macros (define-syntax unless (syntax-rules (unless) ((unless condition then) (if (not condition) then))))Friday, January 25, 13
  57. 57. Macros (define-syntax unless (syntax-rules (unless) ((unless condition then) (if (not condition) then))))Friday, January 25, 13
  58. 58. Macros (define-syntax unless (syntax-rules (unless) ((unless condition then) (if (not condition) then))))Friday, January 25, 13
  59. 59. continuations (call-with-current-continuation (lambda (escape) (display "First") (escape 42) (display "Second")))Friday, January 25, 13
  60. 60. ... and keep continuing (define continue 0) (crazy-display (call/cc (lambda (escape) (set! continue escape) 79))) (continue 42)Friday, January 25, 13
  61. 61. looping (let loop ((i 0)) (if (= i 10) () (begin (display i) (newline) (loop (+ i 1)))))Friday, January 25, 13

×