CIS 352   —   SPRING 2009

Homework 1


Coverage

Logistics

What to turn in

How to turn it in


EXERCISES

Exercise 1   (20 points)

For each of the following lists, give an expression (using only the symbols a, b, c), and d; the procedure cons); and the empty list ()) that evaluates to it. For example, (a b c)) can be constructed as: (cons 'a (cons 'b (cons 'c ()))) (Note: For the purpose of this question, the answer (cons 'a '(b c))) would be considered incorrect, because it fails the above criteria.)

  1. (a (b) c d)
  2. ((a b) c d)
  3. (a (b (c)) d)
  4. (a (b (c (d))))
  5. (((a) b) (c (d)))

Exercise 2   (20 points)

Extract the symbol a from each of the following lists, using only car, cdr, and their hybrid forms (e.g., caddr). For example, a can be exracted from (b (a c)) using (car (car (cdr '(b (a c))))) or the abbreviated (caadr '(b (a c))).
  1. (b c d a)
  2. ((b c) (a d))
  3. (c ((a)) b)
  4. (b (d (a c)))
  5. (d (c (b) a))

Exercise 3   (18 points)

Suppose we have the following definitions:
  (define thing-one '(1 (a . 2) (((z)))) ) 
  (define thing-two '(((b 3) #t) w (x y (z)) (4 . 6)) ) 
Draw pictures (as in lecture and Dybvig Exercise 2.2.5) of the internal list structures for both thing-one and thing-two.

Exercise 4   (24 points)

A Pythagorean triple is a collection of three positive integers (say, m, n, and p) such that m2 +n2 = p2. For example, 3, 4 and 5 form a Pythagorean triple (since 32 +42 = 52), as do 65, 72, and 97.
  1. (7 points)   Write a Scheme procedure allPositive? that takes three integers and returns #t if all three are poitive; it should return #f otherwise.

  2. (17 points)   Write a Scheme procdure pythTriple? that takes three integers (x, y, and z and determines whether or not x, y, and z form a Pythagorean triple.

Your procedure should work correctly regardless of the order of the arguments or their sign. For example,

(pythTriple 3 5 4)
(pythTriple 4 5 3)
should both return #t, whereas
(pythTrple 3 -4 5)
should return #f.


Exercise 5   (18 points)

Use cond to write a Scheme procedure identify that takes a single argument and determines whether it is: In each case, it should return a string indicating the datum's type. For example:
> (identify 6/7) 
"number" 
> (identify car) 
"procedure" 
> (identify 'car) 
"symbol" 
> (identify '(3 . 5)) 
"dotted pair" 
> (identify #\a) ;; #\a is a char 
"unknown" 

Back to the CIS 352 Homepage
Jim Royer /