CS61A Summer 2011 Lab 13
1. Lazy evaluator. Abelson and Sussman, exercises 4.27 and 4.29.
2. In this exercise we learn what a continuation is.
Suppose we have the following definition:
(define (square x cont)
(cont (* x x)))
Here x is the number we want to square, and cont is
the procedure to which we want to pass the result. Now try these
> (square 5 (lambda (x) (+ x 2)))
> (square 5 (lambda (x) (square x (lambda (x) x))))
> (square 5 display)
> (define foo 3)
> (square 5 (lambda (x) (set! foo x)))
Don't just type them in -- make sure you understand why they work!
Abelson and Sussman, exercises 4.55 and 4.62:
4.55: Give simple queries that retrieve the following information from the data base:
All people supervised by Ben Bitdiddle;
The names and jobs of all people in the accounting division;
The names and addresses of all people who live in Slumerville.
4.62: Define rules to implement the last-pair operation of exercise 2.17, which returns a list containing the last element of a nonempty list. Check your rules on queries such as
(last-pair (3) ?x)
(last-pair (1 2 3) ?x)
(last-pair (2 ?x) (3))
Do your rules work correctly on queries such as (last-pair ?x (3))?
For the lab exercises and the homework problems that involve writing queries or rules, test your solutions using the query system. To run the query system and load in the sample data:
You're now in the query system's interpreter. To add an assertion:
(assert! (foo bar))
To add a rule:
(assert! (rule (foo) (bar)))
Anything else is a query.