CSCI 301 L03 Worksheet

Lecture 3 - Exercises

Part A - cons, car, cdr

  1. What does (car (cons 1 2)) evaluate to?
  2. What does (cdr (cons 1 2)) evaluate to?
  3. What does (cdadr (cons 1 (cons (cons 2 3) 4))) evaluate to?

Part B - Lists, list, quoting, caar/cadr/etc.

  1. What does (cdr (list 1 2)) evalute to?
  2. What list is this equivalent to? (cons 1 (cons 2 (cons 3 '())))

Suppose we (define s (list 1 2 3 4)).

  1. What does (cadr s) evaluate to?
  2. What does (cddr s) evaluate to?

Part C - Posing Solutions Recursively

  1. Define a procedure sum that returns the sum of the elements in a list; assume the list contains only numbers.
  2. Define a procedure avg that returns the average of the elements in a list; you may use any function(s) we’ve written so far, even if you don’t currently have a working implementation.