Exercise 4.43

Source code for Exercise 4.43.
I took three attempts to get to this solution. In the first I represented each sailor as a list of daughter and yacht, but I realised when the answer is displayed we can’t actually identify who the father is so I added surname.
The second attempt specified (require (distinct? (map daughter sailors)) and (require (distinct? (map yacht sailors)) but after a bit of testing they are actually unnecessary.

(define (sailors)
  
  (define father first)
  (define daughter second)
  (define yacht third)
  (define (different-names father)
    (not (eq? (daughter father) (yacht father))))
  
  (let ((moore   (list 'moore 'mary-ann              'lorna))
        (hood    (list 'hood  'melissa                'gabrielle))
        (hall    (list 'hall  (amb 'gabrielle 'lorna) 'rosalind))
        (downing (list 'downing (amb 'gabrielle 'lorna 'rosalind) 'melissa))
        (parker (list 'parker  (amb 'gabrielle 'lorna 'rosalind) 'mary-anne)))
    (let ((gabrielle-father (amb hall downing parker))
          (lorna-father (amb hall downing parker)))
      (require (eq? (daughter gabrielle-father) 'gabrielle))
      (require (eq? (daughter lorna-father) 'lorna))
      (require (different-names lorna-father))
      (require (different-names gabrielle-father))
      (require (eq? (daughter parker)
                    (yacht gabrielle-father)))
      lorna-father)))

(sailors)
'(downing lorna melissa)

Lorna is Downing’s daughter.

For the second part of the question though – if we ignore the knowledge of Mary Ann’s surname – the sailors are established slightly differently, because now Mary Anne could be the daughter of Moore or another sailor. Notice the extra amb expression for moore and the addition of 'mary-anne to the potential daughters of hall, downing and parker. This time it is important to ensure the distinct values of daughters and yachts.

(define (sailors-2)
  
  (define father first)
  (define daughter second)
  (define yacht third)
  (define (different-names father)
    (not (eq? (daughter father) (yacht father))))
  
  (let ((moore   (list 'moore (amb 'mary-ann 'gabrielle 'lorna 'rosalind) 'lorna))
        (hood    (list 'hood  'melissa                 'gabrielle))
        (hall    (list 'hall  (amb 'gabrielle 'mary-ann 'lorna)  'rosalind))
        (downing (list 'downing (amb 'gabrielle 'mary-ann 'lorna 'rosalind) 'melissa))
        (parker  (list 'parker  (amb 'gabrielle 'lorna  'rosalind) 'mary-ann)))
    (require (different-names moore))
    (let* ((gabrielle-father (amb hall downing parker moore))
           (lorna-father (amb hall downing parker moore))
           (fathers (list moore hood hall downing parker))
           (daughters (map daughter fathers))
           (yachts    (map yacht    fathers)))
      (require (distinct? daughters))
      (require (distinct? yachts))
      (require (not (equal? daughters yachts)))
      (require (eq? (daughter gabrielle-father) 'gabrielle))
      (require (eq? (daughter lorna-father) 'lorna))
      (require (different-names lorna-father))
      (require (different-names gabrielle-father))
      (require (eq? (daughter parker)
                    (yacht gabrielle-father)))
      lorna-father)))

(length (amb-collect (sailors-2)))
2
(amb-collect (sailors-2))
'((downing lorna melissa) (parker lorna mary-anne))
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s