Books & Authors
Books have: title : String author : Author
Authors have: name : String books : [Listof Book]
;; A Book is (book% String Author) ;; An Author is (author% String [Listof Book])
But this is wrong: the Rose has written a book, but the author object doesn’t know about it.
Do we need books to know the author? Yes.
;; A Book is (book% String Author) (define-class book% (fields title author)) ;; An Author is (author% String [Listof Book]) (define-class author% (fields name books)) (define rose (author% "Rose" empty)) (define reign (book% "Reign of Roquet" rose))
Question: Does reign contain a copy of rose, or are they all the same rose? Answer: always the same, because we use the name rose, we didn’t construct a new one.
How does it print?
#0=(object:author% "Rose" (list (object:book% "Reign of Roquet" #0#)))
#0=(object:book% "Reign of Roquet" (object:author% "Rose" '(#0#)))
See graph-style printing.
But every times we construct a book with an author, we want to use add-book. So, let’s use the constructor.