Homework 7: Due Wednesday March 30, 2011

Scheme Part 1

Launch Stk Scheme and experiment by evaluating the following expressions and observing the results.

You can copy and paste into the interpreter window. The arrow keys can be used to recall and edit previously entered expressions.

 (+ 4 5 2 7 5 2) (/ 36 6 2) (+ (* 2 2 2 2 2) (* 5 5)) (cdr '(7 6 5)) (car '(7 6 5)) (cdr '(this (is not) so hard)) (car '(this (is not) so hard)) (caadr '(this (is not) so hard)) (cons 1 '(2 3 4)) (cons '(1 2 3) '(4 5 6)) (list 1 2 3 4) (list '(1 2 3) '(4 5 6)) (cons (cons 'a ()) '(b c)) (append '(1 2 3) '(4 5 6)) (list? '(a b c)) (list? 5) (symbol? 'x) (symbol? 5)

1. Evaluate the fillowing expressions and briefly describe the general differences between the =, eq?, and equal? functions.

 (= 7 (+ 2 5)) (eq? 7 (+ 2 5)) (equal? 7 (+ 2 5)) (= '(a b c) '(a b c)) (eq? '(a b c) '(a b c)) (equal? '(a b c) '(a b c)) (eq? '(a (b c) (d)) '(a (b c) (d))) (equal? '(a (b c) (d)) '(a (b c) (d)))

2. a) Evaluate the following expression and briefly describe what the define function does.

(define x '(a b c))

Evaluate the following expression:

(if (symbol? (car x)) (cdr x) (caar x))

b) What result would occur if the condition part of the if expression above is false (#f)?

c) Both define and if are considered to be "special forms". What is it about these functions that is different than normal Scheme functions?

3. a) Write an expression in Scheme that includes all operations needed to calculate the following:

(1/2) * 42 + 20 - 3*5

b) What result is returned when this expression is evaluated?

Use the following lists to complete problems 4 - 5:

(a (b (c d)) (e) )      ; call this list L1

((1 ()) ((2) (3) 4) 5)  ; call this list L2

4. a) What is returned by each of the following (indicate any that result in an error).

(car (cdr L1))
(car (cdr L2))

(car (car L1))
(car (car L2))

(cdr (car (cdr (cdr L1))))
(cdr (car (cdr (cdr L2))))

b) Rewrite each of the above with the abbreviated form of car/cdr.

5. Write car/cdr expressions that return the following:

b from L1

e from L1

4 from L2

6. Write a Scheme expression that will build the following list by using the cons function. The arguments for the cons function calls in your answer must be atoms, empty lists, or other cons's. (You can't just do something like (cons '(x) '(y z)) because (x) and (y z) are not atoms, empty lists, or calls to cons)

((x) y z)

7. Draw diagrams (with nodes and pointers) for lists L1 and L2 from above.