;; 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 list-linear-struct) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) ;; [See note on hws page, about downloading (not pasting) .rkt files.] ; Data Definiton: (define-struct book (title author num-pages is-copyrighted?)) ; make-book : string, string, natural, boolean -> book ;;; Data Def'n: ; A monst-def ("monster defender") is either: ; - String ; - Book ; - (make-amplifier [number] [MonstDef] [String]) ; interpretation: an amplifier is a device that lets you plug in ; some other monster-defender, and it amplifies it by `strength`. ; When using it, it makes a sound. ; Of course, because amplifiers are themselves monst-defs, you can plug one amplifier into another. (define-struct amplifier (strength def sound)) ;;; TASK: write examples of the data "go away you wicked monster!" "shoo!" (make-book "The Cat in the Hat" "Seuss" 37 #true) (make-amplifier 17 "shoo!" "cough") (make-amplifier 2 (make-amplifier 17 "shoo!" "cough") "clink-clank") (make-amplifier 0 (make-amplifier 2 (make-amplifier 17 "shoo!" "cough") "clink-clank") "oops") ; We could draw these as trees... ; but really they'd always be long sparse trees. ; In spirit, they are actually lists. ;;; TASK write the template for func-for-monst-def ; func-for-monst-def : monst-def -> ?? ; (define (func-for-monst-def a-md) (cond [(string? a-md) ...] [(book? a-md) (book-title a-md) (book-author a-md) (book-num-pages a-md) (book-is-copyrighted? a-md)] [(amplifier? a-md) (amplifier-strength a-md) (func-for-monst-def (amplifier-def a-md)) (amplifier-sound a-md)])) ; TASK: Write effectiveness : monst-def -> number? ; We say that the effectiveness of a monster-defender is: ; - (for strings) the length of the string you shout (f ; - (for books) the #pages of the book, plus 10 if it's copyrighted ; - (for amplifiers) the amplifier-strength times the strength of the internal monster-defender, minus the ; number of characters in its sound. (check-expect (effectiveness "go away you wicked monster!") 27) (check-expect (effectiveness "shoo!") 4) (check-expect (effectiveness (make-book "The Cat in the Hat" "Seuss" 37 #true)) 47) (check-expect (effectiveness (make-book "The Soul of Wit" "Barland" 0 #false) 0)) (check-expect (effectiveness (make-book "Chat Roulette for Dummies" "Sir Preiz" 23 #false) 23)) (check-expect (effectiveness (make-amplifier 17 "shoo!" "k-k-k")) (- (* 17 (strength "shoo!")) 5)) (check-expect (effectiveness (make-amplifier 17 (make-book "The Cat in the Hat" "Seuss" 37 #true)) "k-k-k")) (- (* 17 (strength "shoo!")) 5)) (check-expect (effectiveness (make-amplifier 2 (make-amplifier 17 "shoo!" "cough") "clink-clank")) (- (* 2 (effectiveness (make-amplifier 17 "shoo!" "cough"))) 11)) (check-expect (effectiveness (make-amplifier 0 (make-amplifier 2 (make-amplifier 17 "shoo!" "cough") "clink-clank") "oops")) -4) (check-expect (effectiveness (make-amplifier 3 (make-amplifier 2 (make-amplifier 17 "shoo!" "cough") "clink-clank") "pew, pew")) (- (* 3 (effectiveness (make-amplifier 2 (make-amplifier 17 "shoo!" "cough") "clink-clank"))) 8)) ; TASK: Write total-sound : monst-def -> string ; Return the sound made, when using a mosnst-def. ; This is: ; - (for strings) the string itself (it's being shouted!) ; - (for books) the string "(flip, flip)", the sound of pages being turned ; [alternately: make it a string of "."s, one for each page] ; - (for amplifiers): the amplifier's `sound` followed by the total-sound of its `def` followed by its `sound` (again).