A Scheme Refresher (Functional Subset) Prabhaker Mateti
Overview of Scheme Guy Steele and Gerald Sussman, MIT, 1975 Interpreted or compiled to machine code REPL (Read, Eval, Print, Loop) Like Python, MatLab Lisp syntax + lexical scope Lists are a fundamental built-in data type Everything in prefix form: (+ a b) instead of the infix a + b Procedures represented by computable data structures CS784 2SchemeRefresher
Standards Docs for Scheme Official Standard: The IEEE standard, (R1995) standards.ieee.org/standards.ieee.org/ De Facto Std: R6RS (2007) Revised^6 Report on the Algorithmic Language Scheme Older R5RS (1998) Documents/Standards/R5RS/ Documents/Standards/R5RS/ CS784 3SchemeRefresher
“Implementations” of Scheme Many (~100 ?) that meet R5RS and R6RS. On Linux, Windows, … CS784 choice: Previously (< June 2010) known as PLT-Scheme DrRacket, … “Determine language from source” EOPL3 Many extensions CS784 4SchemeRefresher
Scheme Reading Teach Yourself Scheme in Fixnum Days, Dorai Sitaram, free download, neu.edu/home/dorai/ (For programmers.) neu.edu/home/dorai/ How to Design Programs, Felleisen, Bruce Findler, Flatt and Krishnamurthi, MIT Press, on-line (For beginners.) Scheme Wiki: community.schemewiki.org/community.schemewiki.org/ CS784 SchemeRefresher5
Examples, Discussion, … CS784 SchemeRefresher6
Examples-1 (define x 10) (define y 20) (cons x y) (cdr (cons x y)) (symbol? '123) (string->symbol "one, two") CS784 7SchemeRefresher
Examples-2 1.(define list-length 2. (lambda (lst) 3. (if (null? lst) (+ 1 (list-length (cdr lst)))))) 1.(define remove-first 2. (lambda (s los) 3. (if (null? los) 4. '() 5. (if (eqv? (car los) s) 6. (cdr los) 7. (cons (car los) (remove-first s (cdr los))))))) CS784 8SchemeRefresher
Example-3 EOPL3 p19 1.(define occurs-free? 2. (lambda (var exp) 3. (cond 4. ((symbol? exp) (eqv? var exp)) 5. ((eqv? (car exp) 'lambda) 6. (and 7. (not (eqv? var (car (cadr exp)))) 8. (occurs-free? var (caddr exp)))) 9. (else 10. (or 11. (occurs-free? var (car exp)) 12. (occurs-free? var (cadr exp))))))) CS784 9SchemeRefresher
Example-4 EOPL3 p21 1.(define subst-in-s-exp 2. (lambda (new old sexp) 3. (if (symbol? sexp) 4. (if (eqv? sexp old) new sexp) 5. (subst new old sexp)))) (subst 'a 'b '((b c) (b () d))) '((a c) (a () d)) CS784 10SchemeRefresher
Example-5 EOPL3 p23 1.(define number-elements-from 2. (lambda (lst n) 3. (if (null? lst) '() 4. (cons 5. (list n (car lst)) 6. (number-elements-from (cdr lst) (+ n 1)))))) 7.(define number-elements 8. (lambda (lst) 9. (number-elements-from lst 0))) (number-elements '(a b c d e)) '((0 a) (1 b) (2 c) (3 d) (4 e)) CS784 11SchemeRefresher
Built-In Data Types Booleans: #t #f Numbers Characters Strings (Unicode) Bytes and Byte Strings Symbols Keywords Pairs and Lists Vectors Hash Tables Boxes (a container for a value) Void and Undefined CS784 12SchemeRefresher
Four Types of Expressions Constants: numbers, booleans. Variables: names for values. Created using the special form define Special forms have special rules for evaluation. Cannot be redefined 15 “magic words” and, begin, case, cond, define, do, if, lambda, let, let*, letrec, or, quasiquote, quote, set! a special form is not a first-class object like a procedure Combination: (operator operands) "function calls" or "procedure applications." 13 CS784 SchemeRefresher
Mantras of Scheme Every expression has a value except for errors, infinite loops, and define special form Computing the value of a combination (operator operands …) Compute all sub-expressions (in any order) Apply the value of the first to the values of the rest Applicative Order The value of a lambda expression is a procedure CS784 SchemeRefresher14
Expressions Literals 2, 22/7, 3.1, "abc", #t '(define x 12) Variables (define x 12) x Function Calls (aka Procedure Applications) ( ‹id› ‹expr›* ) CS784 15SchemeRefresher
Symbols A symbol is an atomic value. An expression that starts with ' and continues with an identifier. Distinct from identifiers that name variables in the program text. Distinct from strings (sequence of characters). Examples (symbol? 'blah) #t (string->symbol "one, two") |one, two| CS784 16SchemeRefresher
Pairs cons procedure: (cons 10 20) constructs a pair (10. 20) (cons '() 'blah) ((). blah) (cons "blah" '()) ("blah") (cons 'blah '()) (blah) car procedure: first value cdr procedure: second value (cdr (cons 10 20)) 20 (pair? '(1 2)) #tpair? CS784 17SchemeRefresher
Lists A list is recursively defined: the constant null, ornull a pair whose second value is a list. Ordered sequence of elements of arbitrary types (Heterogeneous) (list? (cons alpha (cons 2 '()))) #tlist?cons (list? (cons alpha 2)) #flist?cons CS784 18SchemeRefresher
Procedure Call/Application ( ‹id› ‹expr›* ) (proc exp1 exp2 exp3...) is in other languages proc(exp1 exp2 exp3...) (equal? 6 "half dozen")equal? ((f #t 3) (string? s) 6)string? Order of evaluation of the sub-expressions is deliberately left unspecified by Scheme. cf. C is silent about it. cf. Java specifies a left to right processing. CS784 19SchemeRefresher
procedure: (eq? v 1 v 2 ) Return #t if v1 and v2 refer to the same object, #f otherwise (eq? 'yes 'yes) #teq? (eq? "yes" "yes" ) #f (eq? (cons 1 2) (cons 1 2)) #f CS784 20SchemeRefresher
procedure: (eqv? v 1 v 2 ) Two values are eqv? if and only if they are eq?,eqv?eq? except for number and character data typesnumbercharacter Examples (eqv? (expt 2 100) (expt 2 100)) #t (eq? (expt 2 100) (expt 2 100)) #f CS784 21SchemeRefresher
procedure: (equal? v 1 v 2 ) Two values are equal? if and only if they are eqv?,equal?eqv? except for strings, byte strings, numbers, pairs, mutable pairs, vectors, hash tables, and inspectable structures. A rule of thumb is that objects are generally equal? if they print the same. For precise details, see the docs of Scheme implementation you are using. CS784 22SchemeRefresher
eq? eqv? equal? (equal? (expt 2 100) (expt 2 100)) #t (eqv? (expt 2 100) (expt 2 100)) #t (eq? (expt 2 100) (expt 2 100)) #f (equal? (make-string 3 #\z) (make-string 3 #\z)) #t (eqv? (make-string 3 #\z) (make-string 3 #\z)) #f (eq? (make-string 3 #\z) (make-string 3 #\z)) #f CS784 23SchemeRefresher
procedure: (vector v...) Returns a new mutable “array” with the slots initialized to contain the given v … in order. Index starts at 0. (vector 'a 20 "yes") #(a 20 "yes") (define v (vector 'a 20 "yes")) (vector-ref v 2) "yes“ (vector-length v) 3 (vector->list v) (a 20 "yes") (list->vector '(a 20 "yes")) #(a 20 "yes") (build-vector 5 add1) '#( )build-vectoradd1 CS784 24SchemeRefresher
Special Forms A special form is an expression that follows special evaluation rules. Lambda Expressions Definitions Assignments Conditionals Sequencing Iteration CS784 25SchemeRefresher
(define(define id expr) binds id to the result of expr (define x 10) (define v (vector 'a 20 "yes")) CS784 26SchemeRefresher
(define (head args) body...+)define binds id, in the head, to a procedure. (define (g x) (+ x 1)) (define ((f x)) (+ x 20)) ((f 10)) 30 (f 10) # CS784 27SchemeRefresher
(define fnid (lambda (id …) …) 1.(define (factx n) 2. (if (= n 0) 1 3. (* n (facty (- n 1))))) 4.(factx 4) 24 5.(define facty 6. (lambda (n) 7. (if (= n 0) 1 8. (* n (facty (- n 1)))))) 9.(facty 4) (equal? factx facty) #f 11.(eqv? factx facty) #f 12.(eq? factx facty) #f CS784 28SchemeRefresher
Procedures are first-class data The definition of a procedure is stored as a data structure. Can pass them as arguments to other procedures. A procedure can create and return another procedure. CS784 29SchemeRefresher
define-syntax (define-syntax id expr) 1.(define-syntax foo 2. (syntax-rules () 3. ((_ a...) 4. (printf "~a\n" (list a...))))) 5.(foo ) ( ) (define-syntax (head args) body...+)define-syntax 1.(define-syntax (bar syntax-object) 2. (syntax-case syntax-object () 3. ((_ a...) 4. #'(printf "~a\n" (list a...))))) 5.(bar ) ( ) CS784 30SchemeRefresher
(define-syntax equal?? …) 1. (define-syntax equal?? 2. (syntax-rules () 3. ((_ test-exp correct-ans) 4. (let ((observed-ans test-exp)) 5. (if (not (equal? observed-ans correct-ans)) 6. (printf "~s returned ~s, should have returned ~s~%" 7. 'test-exp 8. observed-ans 9. correct-ans) 10. (printf "pmateti ~s OK~%" 11. 'test-exp)))))) CS784 31SchemeRefresher
(apply proc v... ) → anyapply Applies proc using the content of v... as the (by-position) arguments.... (apply '(3)) 6 (define compose (lambda (f g) (lambda args (f (apply g args))))) ((compose sqrt *) 12 75) 30 CS784 32SchemeRefresher
(lambda formals body) A lambda expression evaluates to an anonymous procedure. (lambda (x) (+ 12 x)) # ((lambda (x) (+ 12 x)) 20) 32 ((lambda (x y) (list y x)) 1 2) '(2 1)list ((lambda (x [y 5]) (list y x)) 1 2) '(2 1)list CS784 33SchemeRefresher
(if test then-exp else-exp)if If test produces any value other than #f, then then-exp is the result. Otherwise, else- exp is the result. (if (> 3 2) (- 3 2) (+ 3 2)) 1 (if 'we-have-no-bananas "yes" "no") "yes" CS784 34SchemeRefresher
(cond clause 1 clause 2...) clause ::= ( test expression ) (cond [(positive? -5) (error "doesn't get here")] [(zero? -5) (error "doesn't get here, either")] [(positive? 5) 'here]) here (cond [(member 2 '(1 2 3)) => (lambda (x) (map - x))]) (-2 -3) (cond [(member 2 '(1 2 3))]) (2 3) CS784 35SchemeRefresher
(case key clause 1 clause 2...) clause ::= ( keyList expression ) (case (- 7 5) [(1 2 3) 'small] [( ) 'big]) 'small (define (classify c) (case (char-general-category c)char-general-category [(ll lu lt ln lo) "letter"] [(nd nl no) "number"] [else "other"])) (classify #\A) "letter " (classify #\1) "number" (classify #\!) "other" CS784 36SchemeRefresher
Local Binding: let (let ([id val-expr]...) body...+)let Evaluates the val-exprs left-to-right, creates a new location for each id, and places the values into the locations. It then evaluates the bodys, in which the ids are bound. 1.(let ([x 5]) 2. (let ([x 2] 3. [y x]) 4. (list y x))) '(5 2) CS784 37SchemeRefresher
Local Binding: let (let proc-id ([id init-expr]...) body...+)let Defines a local procedure. Evaluates the init-exprs; these become arguments to the proc. The ids must be distinct. (let fac ([n 10]) (if (zero? n) 1 (* n (fac (sub1 n))))) CS784 38SchemeRefresher
Local Binding: let* (let* ([id val-expr]...) body...+)let* Similar to let, but evaluates the val-exprs one by one, creating a location for each id as soon as the value is available. The ids are bound in the remaining val-exprs as well as the bodys, and the ids need not be distinct; later bindings shadow earlier bindings.let 1.(let* ([x 1] 2. [y (+ x 1)]) 3. (list y x)) (2 1) CS784 39SchemeRefresher
Local Binding: letrec (letrec ([id val-expr]...) body...+)letrec Similar to let, but the locations for all ids are created first and filled with #, and all ids are bound in all val-exprs as well as the bodys. The ids must be distinct. 1.(letrec ((a b) (b 34) (c (+ b 5))) 2. (list a b c)) (# 34 39) 3.(letrec ([is-even? (lambda (n) 4. (or (zero? n) 5. (is-odd? (sub1 n))))] 6. [is-odd? (lambda (n) 7. (and (not (zero? n)) 8. (is-even? (sub1 n))))]) 9. (is-odd? 11)) #t CS784 40SchemeRefresher
Comparison: let, let*, letrec (let/let*/letrec ((v1 e1 ) (v2 e2 )…(vn en )) body ) let no vi is created until all ei are evaluated. none of ei can refer to any vi let* e1 is evaluated; v1 created, bound to e1; e2 is evaluated; v2 created, bound to e2; …; ej can refer to earlier vi, i < j. letrec vi are created with #undefined as their value. with the above in effect, e1, …, en are evaluated l-to-r each vi is now bound to ei CS784 41SchemeRefresher