41 lines
1.3 KiB
Racket
41 lines
1.3 KiB
Racket
;; Exercise 4.42
|
|
|
|
(define (fathers-daughters)
|
|
|
|
(define father car)
|
|
(define yacht cadr)
|
|
(define daughter caddr)
|
|
|
|
(define (lookup-daughter f fathers)
|
|
(daughter (assq f fathers)))
|
|
|
|
(define (lookup-yacht f fathers)
|
|
(yacht (assq f fathers)))
|
|
|
|
(define (lookup-father d fathers)
|
|
(cond ((null? fathers) '())
|
|
((eq? d (daughter (car fathers)))
|
|
(car (car fathers)))
|
|
(else (lookup-father d (cdr fathers)))))
|
|
|
|
;; Optimisation. Put everything we already know in a table. Each
|
|
;; row is father, yacht, daughter.
|
|
(define fathers
|
|
(list
|
|
(list 'moore 'lorna 'mary)
|
|
(list 'hood 'gabrielle 'melissa)
|
|
(list 'downing 'melissa (amb 'lorna 'gabriella 'rosalind))
|
|
(list 'hall 'rosalind (amb 'lorna 'gabrielle))
|
|
(list 'parker 'mary (amb 'lorna 'gabrielle 'rosalind))))
|
|
|
|
(require (distinct? (list (lookup-daughter 'moore fathers)
|
|
(lookup-daughter 'hood fathers)
|
|
(lookup-daughter 'downing fathers)
|
|
(lookup-daughter 'hall fathers)
|
|
(lookup-daughter 'parker fathers))))
|
|
;; Gabrielle's father owns the yacht named after parker's daughter
|
|
(require (eq? (lookup-yacht (lookup-father 'gabrielle fathers) fathers)
|
|
(lookup-daughter 'parker fathers)))
|
|
fathers)
|
|
|