Upgrade to Pro — share decks privately, control downloads, hide ads and more …

R⁷RS Scheme

R⁷RS Scheme

An introduction to the Scheme programming language. Topics include abstract syntax trees (ASTs), proper tail recursion, quotation, homoiconicity, closures, macros and continuations.

After going through the slides, we went through some example Scheme programs. We spent some time looking at how we could implement things like exception handling, object orientation, non-deterministic programming and so on using Scheme primitives.

Link to MeetUp:
http://www.meetup.com/Functional-escape/events/126170252/

Link to Mickey Scheme:
https://github.com/cslarsen/mickey-scheme

Christian Stigen Larsen

August 29, 2013
Tweet

More Decks by Christian Stigen Larsen

Other Decks in Programming

Transcript

  1. R7RS SCHEME C hr i st i an S t

    i gen Larsen 2013 - 08 - 29
  2. «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.» From the SC HEME standards
  3. The entire language can be built with quote, if, lambda,

    set!  + variables, constants and procedure calls
  4. public static int fact(int n) { if ( n ==

    0 ) return 1; else return n*fact(n-1); }
  5. if == n 0 1 n * fact - 1

    n public static int fact(int n) { if ( n == 0 ) return 1; else return n*fact(n-1); }
  6. if == n 0 1 n * fact - 1

    n public static int fact(int n) { if ( n == 0 ) return 1; else return n*fact(n-1); } (if (equal? n 0) 1 (* n (fact (- n 1))))
  7. (define (fact n) (if (zero? n) 1 (* n (fact

    (- n 1))))) if == n 0 1 n * fact - 1 n
  8. (define (fact n) (if (zero? n) 1 (* n (fact

    (- n 1))))) if == n 0 1 n * fact - 1 n
  9. (define (fact n) (if (zero? n) 1 (* n (fact

    (- n 1))))) if == n 0 1 n * fact - 1 n
  10. - 1 n fact n * acc TAIL RECURSIVE (define

    (fact n acc) (if (zero? n) acc (fact (- n 1) (* n acc))))
  11. - 1 n fact n * acc TAIL RECURSIVE (define

    (fact n acc) (if (zero? n) acc (fact (- n 1) (* n acc)))) (define (fact n acc) (if (zero? n) acc (fact (- n 1) (* n acc))))
  12. (quasiquote (* 2 ,(+ 1 2))) > ‘(* 2 3)

    `(* 2 ,(+ 1 2))) > ´(* 2 3)
  13. (define code ‘(let ((age 36)) (* age 365))) (print code)

    > ‘’(define ...)’’ (eval code) > 13140
  14. (let ((name ‘‘foo’’)) (lambda (title) (print title name))) (let ((name

    ‘‘foo’’)) (lambda (title) (print title name))) (let ((name ‘‘foo’’)) (lambda (title) (print title name)))
  15. (let ((name ‘‘foo’’)) (lambda (title) (print title name))) (let ((name

    ‘‘foo’’)) (lambda (title) (print title name))) (let ((name ‘‘foo’’)) (lambda (title) (print title name)))
  16. (define send #f) ; initialize (define recv #f) (let ((secret-message

    ‘‘none’’)) (set! send (lambda (str) (set! secret-message str))) (set! recv (lambda () secret-message))) (send ‘‘Meet me by the docks at midnight’’) (print (recv))
  17. (define send #f) ; initialize (define recv #f) (let ((secret-message

    ‘‘none’’)) (set! send (lambda (str) (set! secret-message str))) (set! recv (lambda () secret-message))) (send ‘‘Meet me by the docks at midnight’’) (print (recv))
  18. A macro is a compile-time change in the AST. It

    is used to control evaluation.
  19. (define-syntax when (syntax-rules () ((when test code ...) (if test

    (begin code ...))))) (define-syntax when (syntax-rules () ((when test code ...) (if test (begin code ...))))) (define-syntax when (syntax-rules () ((when test code ...) (if test (begin code ...))))) (define-syntax when (syntax-rules () ((when test code ...) (if test (begin code ...))))) (define-syntax when (syntax-rules () ((when test code ...) (if test (begin code ...))))) (define-syntax when (syntax-rules () ((when test code ...) (if test (begin code ...)))))
  20. (print (* 10 (call/cc (lambda (c) 12)))) (print (* 10

    (call/cc (lambda (c) 12)))) (print (* 10 (call/cc (lambda (c) 12)))) (print (* 10 (call/cc (lambda (c) 12))))
  21. (define redo #f) ; initialize (print «Result: » (* 10

    (call/cc (lambda (cont) (set! redo cont) 0)))) (redo 10) (redo 12)
  22. (define redo #f) ; initialize (print «Result: » (* 10

    (call/cc (lambda (cont) (set! redo cont) 0)))) (redo 10) (redo 12) (define redo #f) ; initialize (print «Result: » (* 10 (call/cc (lambda (cont) (set! redo cont) 0)))) (redo 10) (redo 12)
  23. (define redo #f) ; initialize (print «Result: » (* 10

    (call/cc (lambda (cont) (set! redo cont) 0)))) (redo 10) (redo 12) (define redo #f) ; initialize (print «Result: » (* 10 (call/cc (lambda (cont) (set! redo cont) 0)))) (redo 10) (redo 12) (define redo #f) ; initialize (print «Result: » (* 10 (call/cc redo-val (lambda (cont) (set! redo cont) 0)))) (redo 10) ; redo-val (redo 12) ; redo-val