A. 1 .........
(define lat?
  (lambda (l)
    (cond [(null? l) #t]
          [(atom? (car l)) (lat? (cdr l))]
          [else #f])))

















































A. 2 ......... Because the empty list is vacuously a list of atoms. It is also vacuously a list of numbers! It is also (vacuously) a list of elephants! Think about your favorite non-Scheme language. It is almost certainly more strongly typed than Scheme. Does it have an entity that can be of type "list of atoms", as well as "list of numbers", as well as "list of elephants"?

















































A. 3 ......... If (car l) is an atom, then l is a list of atoms if and only if (cdr l) is a list of atoms.

















































A. 4 .........
 
(define list-of-ints?
  (lambda (l)
    (cond [(null? l) #t]
          [(integer? (car l)) (list-of-ints? (cdr l))]
          [else #f])))

















































A. 5 ......... The code differs only in the test applied to (car l).

















































A. 6 ......... (car phone-book)

















































A. 7 ......... (car (cdr phone-book))

















































A. 8 ......... (car (cdr (cdr phone-book)))

















































A. 9 ......... (car (cdr (cdr (cdr phone-book))))

















































A. 10 ......... Well, what if Barbara moves away? Then we will want to redefine phone-book:
 
(define phone-book
  '(luke 774-2839 nick 775-0912 valerie 775-9043))

and now (Lukes-phone-number phone-book) gives us Nick's number. Oops.


















































A. 11 ......... The phone book is not empty and Luke is not at the beginning of the phone book, so we take the else case:

(phone-number 'luke '(775-1234 luke 774-2839 nick 775-0912 valerie 775-9043)).

The phone book is not empty and Luke is not at the beginning of the phone book, so we take the else case:

(phone-number 'luke '(luke 774-2839 nick 775-0912 valerie 775-9043)).

Now the phone book is not empty and Luke is at the beginning. So we take the second element of the list, which is Luke's phone number:

774-2839.


















































A. 12 ......... A. Wait a minute, 775-1234 isn't a person!

















































A. 13 ......... A. Ok. The phone book is not empty and 775-1234 is not at the beginning of the phone book, so we take the else case:
(phone-number 'luke '(775-1234 luke 774-2839 nick 775-0912 valerie 775-9043)).
Now the phone book is not empty and 775-1234 is at the beginning. So we take the second element of the list, which is 775-1234's phone number: Luke.

















































A. 14 ......... (car (car phone-book))

















































A. 15 ......... (car (cdr (car phone-book)))

















































A. 16 ......... (car (car (cdr phone-book)))

















































A. 17 ......... (car (cdr (car (cdr phone-book))))

















































A. 18 .........
 
  (define phone-number
     (lambda (person phone-book)
	(cond [(null? phone-book) 'disconnected]
	      [(eq? person (car (car phone-book))) (car (cdr (car phone-book)))]
	      [else (phone-number person (cdr phone-book))])))

















































A. 19 ......... (car (cdr (cdr (car (cdr (cdr phone-and-birthday-book))))))

That's a lot of car's and cdr's!

We can abbreviate it as:

(caddr (caddr phone-and-birthday-book))


















































A. 20 ......... For one thing, there is no such function as caddaddr in Scheme. But more importantly, the two calls to caddr in (caddr (caddr phone-and-birthday-book)) have different roles. The caddr in (caddr phone-and-birthday-book) finds the entry for Nick in the association list. The caddr on the outside of (caddr (caddr phone-and-birthday-book)) extracts the birthday value from Nick's entry.

















































A. 21 ......... (caddr (cadddr phone-and-birthday-book)) This time we use cadddr to find Valerie, but we still use caddr to find her birthday.

















































A. 22 .........
 
  (define phone-number
     (lambda (person phone-book)
	(cond [(null? phone-book) 'disconnected]
	      [(eq? person (car (car phone-book))) (number (car phone-book))]
	      [else (phone-number person (cdr phone-book))])))

















































A. 23 ......... We can define a function for that too:

(define name (lambda (entry) (car entry)))


















































A. 24 .........
 
  (define phone-number
     (lambda (person phone-and-birthday-book)
	(cond [(null? phone-and-birthday-book) 'disconnected]
	      [(eq? person (name (car phone-and-birthday-book)))
	       (number (car phone-and-birthday-book))]
	      [else (phone-number person (cdr phone-and-birthday-book))])))

















































A. 25 ......... Not necessarily. Now the argument to name, etc is not a single entry, it is the whole list. So we should change the definitions to
 
(define name 
  (lambda (phone-and-birthday-book) (car (car phone-and-birthday-book))))
(define number 
  (lambda (phone-and-birthday-book) (cadr (car phone-and-birthday-book))))
(define birthday 
  (lambda (phone-and-birthday-book) (caddr (car phone-and-birthday-book))))

But anyway, it seems nicer to use car and cdr to traverse the list, and to use the help functions to extract the components of an entry.


















































A. 26 ......... They are unnamed constants that appear haphazardly in your code in the form of numbers.

















































A. 27 .........
  1. It is very difficult to understand
  2. It is very difficult to modify (if, for example, we want a 10X10 board)
  3. It is very difficult to adapt so the chessboard can resize dynamically
  4. ...

















































A. 28 ......... Rather than use the built-in car cdr cadr etc. it pays to make your own accessor methods with clear names like firstEntry otherEntries namePart birthdayPart phoneNumberPart etc. This is analogous to naming your constants in Java with names descriptive of their purpose. And it confers the same advantages in terms of making your programs easier to understand and modify. If you change your data representation, you only need to change your accessor methods once and only in one place.