A Brief Introduction to Scheme (II) Philip W. L. Fong pwlfong@cs.uregina.ca Department of Computer Science University of Regina Regina, Saskatchewan, Canada
Lists Scheme II p.1/29
Lists Aggregate data structures are built from lists Examples: (1 2 3) A list of 3 numbers () An empty list ((1 2) (3 4)) A list of 2 lists: (1 2), (3 4) (1 (2 3) 4) A mixed list containing 3 members: 1, (2 3) and 4 (()) A singleton list containing () Scheme II p.2/29
Quoting How do we express list constants? > (1 2 3) Scheme II p.3/29
Quoting How do we express list constants? > (1 2 3) Error! Scheme II p.3/29
Quoting How do we express list constants? > (1 2 3) Error! Scheme thought you were trying to apply the procedure 1 to arguments 2 and 3. Scheme II p.3/29
Quoting How do we express list constants? > (1 2 3) Error! Scheme thought you were trying to apply the procedure 1 to arguments 2 and 3. Thus, quoting... > (quote (1 2 3)) (1 2 3) Scheme II p.3/29
Quoting How do we express list constants? > (1 2 3) Error! Scheme thought you were trying to apply the procedure 1 to arguments 2 and 3. Thus, quoting... > (quote (1 2 3)) (1 2 3) A more convenient form... > (1 2 3) (1 2 3) Scheme II p.3/29
List Constructors () evaluates to an empty list. (cons x L) Argument(s): x: any data object L: a list Return: A list with x as the first member, followed by the members of L. Scheme II p.4/29
List Constructors () evaluates to an empty list. (cons x L) Argument(s): x: any data object L: a list Return: A list with x as the first member, followed by the members of L. Examples: > (cons 1 (2 3)) (1 2 3) Scheme II p.4/29
List Constructors () evaluates to an empty list. (cons x L) Argument(s): x: any data object L: a list Return: A list with x as the first member, followed by the members of L. Examples: > (cons 1 (2 3)) (1 2 3) > (cons 1 ()) (1) Scheme II p.4/29
List Constructors () evaluates to an empty list. (cons x L) Argument(s): x: any data object L: a list Return: A list with x as the first member, followed by the members of L. Examples: > (cons 1 (2 3)) (1 2 3) > (cons 1 ()) (1) > (cons (1 2) (3 4)) ((1 2) 3 4) Scheme II p.4/29
Selector: car (car L) Argument(s): L: a non-empty list Return: the first element of L Examples: > (car (1 2 3)) 1 > (car ((1 2) 3 4)) (1 2) > (car (car ((1 2) 3 4))) 1 > (car (cons 1 (2 3))) 1 Scheme II p.5/29
Selector: cdr (cdr L) Argument(s): L: a non-empty list Return: a list containing all but the first element of L Examples: > (cdr (1 2 3)) (2 3) > (cdr ((1 2) 3 4)) (3 4) > (cdr (cdr ((1 2) 3 4))) (4) > (cdr (cons 1 (2 3))) (2 3) Scheme II p.6/29
How are Lists Represented Internally? L = (1 2 3 4) cons cell L 1 2 3 4 () car field cdr field Scheme II p.7/29
A Complex Example L L = (1 (2 3) ((4) () (5 6))) 1 () 2 3 () () () 4 () 5 6 () Scheme II p.8/29
An Alternative Notation for List Constants The previously discussed notation for list constants (i.e., (1 2 3)) is very user-friendly, but it does not make explicit the internal representation of lists. An equivalent, more explicit, but less user-friendly notation: Dotted Pairs: (car. cdr) Examples: > (2. ()) (2) > (1. (2. ())) (1 2) > ((1. ()). (2. (3. ()))) ((1) 2 3) Scheme II p.9/29
Exercise L L = (1 (2 3) ((4) () (5 6))) 1 () 2 3 () () () 4 () 5 6 () > (car (cdr L)) (2 3) > (car (car (cdr L))) 2 Exercise: How do you fetch 3, 4, 5, and 6? Scheme II p.10/29
Shorthand L L = (1 (2 3) ((4) () (5 6))) 1 () 2 3 () () () 4 () 5 6 () Shorthand for (car (cdr L)) > (cadr L) (2 3) Shorthand for (car (car (cdr L))) > (caadr L) 2 Scheme II p.11/29
Exercise You probably have guessed correctly that you can t write (caaaaaaaaaaaar L). Check out [TSPL3] Sect. 6.3 to see what combinations of car and cdr have shorthands defined. Scheme II p.12/29
Recursion with Lists (list-sum L) Argument(s): L: a list of numbers Return: the sum of all numbers in L Example: > (list-sum (2-1 4)) 5 > (list-sum ()) 0 Scheme II p.13/29
Recursion with Lists (define list-sum (lambda (L) (if (null? L) 0 (+ (car L) (list-sum (cdr L)))))) Scheme II p.14/29
Recursion with Lists (list-sum (2-1 4)) = (if (null? (2-1 4)) 0 (+ (car (2-1 4)) (list-sum (cdr (2-1 4))))) = (+ (car (2-1 4)) (list-sum (cdr (2-1 4)))) = (+ 2 (list-sum (cdr (2-1 4)))) = (+ 2 (list-sum (-1 4))) = (+ 2 (if (null? (-1 4)) 0 (+ (car (-1 4)) (list-sum (cdr (-1 4)))))) = (+ 2 (+ (car (-1 4)) (list-sum (cdr (-1 4))))) = (+ 2 (+ -1 (list-sum (4)))) Scheme II p.15/29
Recursion with Lists (list-sum (2-1 4)) =... = (+ 2 (+ -1 (list-sum (4)))) = (+ 2 (+ -1 (if (null? (4)) 0 (+ (car (4)) (list-sum (cdr (4))))))) = (+ 2 (+ -1 (+ 4 (list-sum ())))) = (+ 2 (+ -1 (+ 4 (if (null? ()) = (+ 2 (+ -1 (+ 4 0))) = 5 0 (+ (car ()) (list-sum (cdr ()))))))) Scheme II p.16/29
More Built-in Procedures Procedure (list x 1 x 2... x n ) (list? x) (null? x) (pair? x) (member x L) (length L) Meaning create a list containing the arguments test if x is a list test if x is the empty list test if x is a cons cell test if x is a member of list L the number of members of list L Scheme II p.17/29
Even More Built-in Procedures (reverse L) Returns a list containing exactly the same objects as the members of L, but in reversed order. (reverse (1 2 3)) (3 2 1) (reverse ()) () (reverse ((1 2) 3)) (3 (1 2)) (append L 1 L 2 ) Returns a list containing both the elements of L 1 and L 2, with those from L 2 following those from L 1 (append (1 2 3) (4 5)) (1 2 3 4 5) (append (1 2) ()) (1 2) (append () (1 2)) (1 2) (append ((1 2) 3) ((4))) ((1 2) 3 (4)) Scheme II p.18/29
Symbols Scheme II p.19/29
Symbols Examples: hello if a3 + zero? Symbols are case insensitive cos and COS are the same symbol Scheme II p.20/29
Quoting Revisited... Expressing symbol constant... > hello Error! Scheme thought you want to retrieve the value of the global variable hello. Again, quoting... > hello hello > (hello world) (hello world) > (a (b c) ((d))) (a (b c) ((d))) Scheme II p.21/29
Symbol-Related Procedures Procedure (symbol? x) (eq? x y) (eqv? x y) Meaning test if x is a symbol test if x & y denote the same symbol test if x & y denote the same symbol or the same number [EOPL2] uses the slower predicate eqv? to test symbol equality. Scheme II p.22/29
Meta-Programming Meta-programming: development of programs that manipulate programs Scheme programs and scheme data share the same representation symbolic lists! (lambda (x) (* x 2)) meta-programming is easy in Scheme Scheme II p.23/29
Example: Eliminating not According to [TSPL3] Sect. 5.3, the conditional form (not x) is equivalent to the following: (if x #f #t) A Scheme interpreter could choose to rewrite all expressions involving not into one without, thereby implementing the higher-level forms with a small number of core forms. Scheme II p.24/29
Example: Eliminating not (rewrite-not-exp exp) Argument(s): exp: a not expression Return: a semantically equivalent if expression Scheme II p.25/29
Example: Eliminating not (rewrite-not-exp exp) Argument(s): exp: a not expression Return: a semantically equivalent if expression Examples: > (rewrite-not-exp (not (zero? y))) (if (zero? y) #f #t) Scheme II p.25/29
Example: Eliminating not (rewrite-not-exp exp) Argument(s): exp: a not expression Return: a semantically equivalent if expression Examples: > (rewrite-not-exp (not (zero? y))) (if (zero? y) #f #t) Implementation: (define rewrite-not-exp (lambda (exp) (list if (cadr exp) #f #t) Scheme II p.25/29
Exercise The expression: (if test-expr true-expr false-expr) can be rewritten into an equivalent form: (cond (test-expr true-expr) (else false-expr)) Implement a procedure (rewrite-if-exp exp) to rewrite a given if expression exp into an equivalent cond expression. Scheme II p.26/29
Exercise Solution (define rewrite-if-exp (lambda (exp) (let ((test-expr (cadr exp) ) (true-expr (caddr exp) ) (false-expr (cadddr exp))) (list cond (list test-expr true-expr) (list else false-expr))))) Scheme II p.27/29
The let Form (let ((var 1 exp 1 ) (var 2 exp 2 )... (var n exp n )) body) 1. All expressions exp 1, exp 2,... exp n are evaluated first. 2. The results are then bound to newly created local variables var 1, var 2,..., var n. 3. With the local variables defined, evaluate body and return its result. Scheme II p.28/29
Lecture Summary This lecture concludes the overview of Scheme as presented in [TSPL3] Chap. 1 2. In this lecture... lists quoting internal representation of lists dotted pairs symbols meta-programming let Scheme II p.29/29