;; The first three lines of this file were inserted by DrRacket. They record metadata ;; about the language level of this file in a form that our tools can easily process. #reader(lib "htdp-beginner-reader.ss" "lang")((modname union-of-structs-before) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) ;;; Data Definiton: ; A book is: (define-struct book (title author num-pages is-copyrighted?)) ; make-book : string string natnum boolean -> book ;;; Examples of the data (define b0 (make-book "Soul and Wit" "Barland" 0 #false)) (define b1 (make-book "The Cat in the Hat" "Seuss" 37 #true)) ;;; Template: ; func-for-book : book -> ... ; (define (func-for-book a-book) (... (book-title a-book) ... (book-author a-book) ... (book-num-pages a-book) ... (book-is-copyrighted? a-book))) ; reading-time : book -> non-negative real ; The amount of time it takes the average reader to read a book (in minutes) ; (define (reading-time a-book) (* (book-num-pages a-book) 2)) (check-expect (reading-time (make-book "The Cat in the Hat" "Seuss" 37 #true)) 74) (check-expect (reading-time b0) 0) ; book->string : book -> string ; Return a string-representation of a book struct, user-friendly. (define (book->string a-book) (string-append (book-title a-book) ", by " (book-author a-book) " (" (number->string (book-num-pages a-book)) "pp)" (if (book-is-copyrighted? a-book) ", ©" ""))) (check-expect (book->string (make-book "The Cat in the Hat" "Seuss" 37 #true)) "The Cat in the Hat, by Seuss (37pp), ©") (check-expect (book->string b0) "Soul and Wit, by Barland (0pp)") ; Create a brand new book, based on an original. ; The result's author will be the original author & the new-author, ; and the result has pages added to the original. ; (define (derive-from original co-author num-new-pages) (make-book (string-append "Son of " (book-title original)) (string-append (book-author original) " & " co-author) (+ (book-num-pages original) num-new-pages) #false)) (check-expect (derive-from b1 "Sauced" 3) (make-book "Son of The Cat in the Hat" "Seuss & Sauced" 40 #false)) (check-expect (derive-from b0 "Freund" 0) (make-book "Son of Soul and Wit" "Barland & Freund" 0 #false)) ;;; Data Def'n: (define-struct dvd (title year length rating)) ; make-dvd : string natnum real? symbol? -> dvd ; The length is in minutes. ;;; Examples of the data: (make-dvd "The Fellowship of the Ring" 2001 178 'PG-13) (make-dvd "Logan Lucky" 2017 118 'PG-13) ;;; Template ; func-for-dvd : dvd -> ... ; (define (func-for-dvd a-dvd) (... (dvd-title a-dvd) ... (dvd-year a-dvd) ... (dvd-length a-dvd) ... (dvd-rating a-dvd))) ; Datatype def'n: ; A lib-item is: ; - a book, OR ; - a dvd, OR ; - 'other ; Examples of the data: (make-book "Cat in Hat" ....) (make-dvd "The Fellowship of the Ring" 2001 178 'PG-13) 'other ; template: code for ANY function which processes a lib-item. (define (func-for-lib-item a-li) (cond [(book? a-li) (... (book-author a-li) (book-title a-li)] [(dvd? a-li) (... (dvd-title a-dvd) (dvd-year a-dvd) (dvd-length a-dvd) (dvd-rating a-dvd))] [(symbol? a-li) ...] )) ;; N.B. we are NOT worrying about inheritance, and the fact that both books and dvds ;; have a field like "title" in common. It is a correct impulse/desire is to use ;; your O.O. design and have a superclass that both `book` and `dvd` might inherit from. ;; However, to keep things straight (and, to understand that we want to be able to ;; process union-types where branches might have *nothing* in common), we won't do ;; any inheritance. (You can look at full-rackets `define-struct` though, to see that ;; it actually can inherit fields, in real-life racket.) ; func-for-lib-item : lib-item -> ...? ; #;(define (func-for-lib-item an-item) (cond [(book? an-item) ...] [(dvd? an-item) ...] [(symbol=? an-item 'other) ...])) ; func-for-lib-item : lib-item -> ...? ; #;(define (func-for-lib-item an-item) (cond [(book? an-item) (... (book-title an-item) ... (book-author an-item) ... (book-num-pages an-item) ... (book-copyrighted? an-item))] [(dvd? an-item) (... (dvd-title an-item) ... (dvd-length an-item) ... (dvd-rating an-item))] [(symbol=? an-item 'other) ...])) ; time-to-waste : lib-item -> non-negative real ; Return how much time I can waste from this library item. (define (time-to-waste an-item) ; time-to-waste: a dvd's run time plus 5min to get it going, ; or (for a book) its reading time plus 1min to read back cover. (cond [(book? an-item) (+ 1 (reading-time an-item))] [(dvd? an-item) (+ 5 (dvd-length an-item))] [(symbol=? an-item 'other) ...]) ;;;; one more notion: struct-of-structs ;; A collectors-edition contains a DVD *and* a book (the deluxe artwork hardback version, perhaps). ; TEMPLATE: same as ever: pull out the two fields. BUT STOP THERE; you'll call helpers ; rather than extract all its fields. (define-struct collectors-edition (disc bk extra-art-pages)) ; collectors-edition : (-> dvd book natnum collectors-edition) ; ripoff: derive-from the book by having Barland add 50% more pages, and replace the DVD with the emoji movie (86min) ; verify-title : collectors-edition -> boolean ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; TODO: a struct-of-unions. ; [Template: Call a helper; ; don't have a cond inside the struct-processing program.]