Solutions to the Exercises due October 29, 2004 2. Solution to Exercise 2: > (first (rest '(1 2 (3 4) 5 6))) 2 > (first (rest (rest '(1 2 (3 4) 5 6)))) (3 4) > (first (rest (rest (rest '(1 2 (3 4) 5 6))))) 5 > (first (rest (first (rest (rest '(1 2 (3 4) 5 6)))))) 4 3. A solution to Exercise 3 with in-line comments: (define (special-list-type? arg) (and ;; is it a list (list? arg) ;; is there at least one element in the list (not (null? arg)) ;; is the first element a number (number? (first arg)) ;; are there at least two elements in the list (not (null? (rest arg))) ;; is the second element of the list a number (number? (first (rest arg))) ;; are there at least three elements in the list (not (null? (rest (rest arg)))) ;; is the third element of the list a list (list? (first (rest (rest arg)))) ;; is the third element a list with at least one element (not (null? (first (rest (rest arg))))) ;; is the third element a list with exactly one element (null? (rest (first (rest (rest arg))))) ;; is the third element a list whose first argument is a string (string? (first (first (rest (rest arg))))) ;; are there at least four elements in the list (not (null? (rest (rest (rest arg))))) ;; is the fourth element in the list a symbol (symbol? (first (rest (rest (rest arg))))) ;; are there exactly four elements in the list (null? (rest (rest (rest (rest arg))))))) 4. Solution to Exercise 4: (define (special-list-type-cond? arg) (cond ((not (list? arg)) (printf "Expecing a list: ~a~%" arg)) ((null? arg) (printf "Expecting a non-empty list: ~a~%" arg)) ((not (number? (first arg))) (printf "Expecting the first element to be a number: ~a~%" arg)) ((null? (rest arg)) (printf "Expecting a list of length > 1: ~a~%" arg)) ((not (number? (first (rest arg)))) (printf "Expecting the second element to be a number: ~a~%" arg)) ((null? (rest (rest arg))) (printf "Expecting a list of length > 2: ~a~%" arg)) ((not (list? (first (rest (rest arg))))) (printf "Expecting the third element to be a list: ~a~%" arg)) ((null? (first (rest (rest arg)))) (printf "Expecting third element to be a non-null list: ~a~%" arg)) ((not (null? (rest (first (rest (rest arg)))))) (printf "Expecting the third element to be list with one item: ~a~%" arg)) ((not (string? (first (first (rest (rest arg)))))) (printf "The only item in the third element should be a string: ~a~%" arg)) ((null? (rest (rest (rest arg)))) (printf "Expecting a list of length 4: ~a~%" arg)) ((not (symbol? (first (rest (rest (rest arg)))))) (printf "Expecting the fourth element to be symbol: ~a~%" arg)) (else (printf "Just what I was expecting: ~a~%" arg)))) > (special-list-type-cond? '(1 2 ("foo") a)) Just what I was expecting: (1 2 (foo) a) > (special-list-type-cond? (list 1 "" (list "foo") 'a)) Expecting the second element to be a number: (1 (foo) a) > (special-list-type-cond? (list 1 7 '("foo" "bar") 'a)) Expecting the third element to be list with one item: (1 7 (foo bar) a)