Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;;;; Problem A1
- ;; 1.)
- (define (fahrenheit->celsius f)
- (* 5/9 (- f 32)))
- (define (celsius->fahrenheit c)
- (+ (* 9/5 c) 32))
- (celsius->fahrenheit (fahrenheit->celsius 212))
- ;;Step 1 - Function application
- (celsius->fahrenheit (* 5/9 (- 212 32)))
- ;;Step 2 - Arithmetic
- (celsius->fahrenheit (* 5/9 180))
- ;;Step 3 - Arithmetic
- (celsius->fahrenheit 100)
- ;;Step 4 - Function application
- (+ (* 9/5 100) 32)
- ;;Step 5 - Arithmetic
- (+ 180 32)
- ;;Step 6 - Arithmetic
- 212
- ;; 2.)
- (define-struct customer (title first last))
- (define (formal* loc)
- (cond [(empty? loc) empty]
- [else (cons (formal (first loc))
- (formal* (rest loc)))]))
- (define (formal c)
- (string-append "Dear "
- (customer-title c)
- " "
- (customer-last c)))
- (formal* (cons (make-customer "Dr." "Olin" "Shivers")
- (cons (make-customer "Mstr." "ZC" "Flatt")
- empty)))
- ;;Step 1 - Function application
- (cond [(empty? (cons (make-customer "Dr." "Olin" "Shivers")
- (cons (make-customer "Mstr." "ZC" "Flatt")
- empty))) empty]
- [else (cons (formal (first (cons (make-customer "Dr." "Olin" "Shivers")
- (cons (make-customer "Mstr." "ZC" "Flatt")
- empty))))
- (formal* (rest (cons (make-customer "Dr." "Olin" "Shivers")
- (cons (make-customer "Mstr." "ZC" "Flatt")
- empty)))))])
- ;;Step 2 - Conditional
- (cond [false empty]
- [else (cons (formal (first (cons (make-customer "Dr." "Olin" "Shivers")
- (cons (make-customer "Mstr." "ZC" "Flatt")
- empty))))
- (formal* (rest (cons (make-customer "Dr." "Olin" "Shivers")
- (cons (make-customer "Mstr." "ZC" "Flatt")
- empty)))))])
- ;;Step 3 - Conditional
- (cond [else (cons (formal (first (cons (make-customer "Dr." "Olin" "Shivers")
- (cons (make-customer "Mstr." "ZC" "Flatt")
- empty))))
- (formal* (rest (cons (make-customer "Dr." "Olin" "Shivers")
- (cons (make-customer "Mstr." "ZC" "Flatt")
- empty)))))])
- ;;Step 4 - Conditional
- (cons (formal (first (cons (make-customer "Dr." "Olin" "Shivers")
- (cons (make-customer "Mstr." "ZC" "Flatt") empty))))
- (formal* (rest (cons (make-customer "Dr." "Olin" "Shivers")
- (cons (make-customer "Mstr." "ZC" "Flatt") empty)))))
- ;;Step 5 - Function Application
- (cons (formal (make-customer "Dr." "Olin" "Shivers"))
- (formal* (rest (cons (make-customer "Dr." "Olin" "Shivers")
- (cons (make-customer "Mstr." "ZC" "Flatt") empty)))))
- ;;Step 6 - Function Application
- (cons (string-append "Dear "
- (customer-title (make-customer "Dr." "Olin" "Shivers"))
- " "
- (customer-last (make-customer "Dr." "Olin" "Shivers")))
- (formal* (rest (cons (make-customer "Dr." "Olin" "Shivers")
- (cons (make-customer "Mstr." "ZC" "Flatt") empty)))))
- ;;Step 7 - Function Application
- (cons (string-append "Dear "
- "Dr."
- " "
- (customer-last (make-customer "Dr." "Olin" "Shivers")))
- (formal* (rest (cons (make-customer "Dr." "Olin" "Shivers")
- (cons (make-customer "Mstr." "ZC" "Flatt") empty)))))
- ;;Step 8 - Function Application
- (cons (string-append "Dear "
- "Dr."
- " "
- "Shivers")
- (formal* (rest (cons (make-customer "Dr." "Olin" "Shivers")
- (cons (make-customer "Mstr." "ZC" "Flatt") empty)))))
- ;;Step 9 - Function Application
- (cons "Dear Dr. Shivers"
- (formal* (rest (cons (make-customer "Dr." "Olin" "Shivers")
- (cons (make-customer "Mstr." "ZC" "Flatt") empty)))))
- ;;Step 10 - Function Application
- (cons "Dear Dr. Shivers"
- (formal* (cons (make-customer "Mstr." "ZC" "Flatt") empty)))
- ;;Step 11 - Function Application
- (cons "Dear Dr. Shivers"
- (cond [(empty? (cons (make-customer "Mstr." "ZC" "Flatt") empty)) empty]
- [else (cons (formal (first (cons (make-customer "Mstr." "ZC" "Flatt") empty)))
- (formal* (rest (cons (make-customer "Mstr." "ZC" "Flatt") empty))))]))
- ;;Step 12 - Conditional
- (cons "Dear Dr. Shivers"
- (cond [false empty]
- [else (cons (formal (first (cons (make-customer "Mstr." "ZC" "Flatt") empty)))
- (formal* (rest (cons (make-customer "Mstr." "ZC" "Flatt") empty))))]))
- ;;Step 13 - Conditional
- (cons "Dear Dr. Shivers"
- (cond [else (cons (formal (first (cons (make-customer "Mstr." "ZC" "Flatt") empty)))
- (formal* (rest (cons (make-customer "Mstr." "ZC" "Flatt") empty))))]))
- ;;Step 14 - Conditional
- (cons "Dear Dr. Shivers"
- (cons (formal (first (cons (make-customer "Mstr." "ZC" "Flatt") empty)))
- (formal* (rest (cons (make-customer "Mstr." "ZC" "Flatt") empty)))))
- ;;Step 15 - Function Application
- (cons "Dear Dr. Shivers"
- (cons (formal (make-customer "Mstr." "ZC" "Flatt"))
- (formal* (rest (cons (make-customer "Mstr." "ZC" "Flatt") empty)))))
- ;;Step 16 - Function Application
- (cons "Dear Dr. Shivers"
- (cons (string-append "Dear "
- (customer-title (make-customer "Mstr." "ZC" "Flatt"))
- " "
- (customer-last (make-customer "Mstr." "ZC" "Flatt")))
- (formal* (rest (cons (make-customer "Mstr." "ZC" "Flatt") empty)))))
- ;;Step 17 - Function application
- (cons "Dear Dr. Shivers"
- (cons (string-append "Dear "
- "Mstr."
- " "
- (customer-last (make-customer "Mstr." "ZC" "Flatt")))
- (formal* (rest (cons (make-customer "Mstr." "ZC" "Flatt") empty)))))
- ;;Step 18 - Function Application
- (cons "Dear Dr. Shivers"
- (cons (string-append "Dear "
- "Mstr."
- " "
- "Flatt")
- (formal* (rest (cons (make-customer "Mstr." "ZC" "Flatt") empty)))))
- ;;Step 19 - Function application
- (cons "Dear Dr. Shivers"
- (cons "Dear Mstr. Flatt"
- (formal* (rest (cons (make-customer "Mstr." "ZC" "Flatt") empty)))))
- ;;Step 20 - Function Application
- (cons "Dear Dr. Shivers"
- (cons "Dear Mstr. Flatt"
- (formal* empty)))
- ;;Step 21 - Function Application
- (cons "Dear Dr. Shivers"
- (cons "Dear Mstr. Flatt"
- (cond [(empty? empty) empty]
- [else (cons (formal (first empty))
- (formal* (rest empty)))])))
- ;;Step 22 - Conditional
- (cons "Dear Dr. Shivers"
- (cons "Dear Mstr. Flatt"
- (cond [true empty]
- [else (cons (formal (first empty))
- (formal* (rest empty)))])))
- ;;Step 23 - Conditional
- (cons "Dear Dr. Shivers"
- (cons "Dear Mstr. Flatt"
- empty))
- ;;;;; Problem A2
- (require 2htdp/image)
- (define-struct ball (x y color))
- ;; Ball = (make-ball Number Number Color)
- ;; Color is one of 'red, 'yellow, 'blue, etc.
- ;;A LOB is one of
- ;; -- (cons Ball LOB)
- ;; -- empty
- (define lob1 (cons (make-ball 50 50 'red)
- (cons (make-ball 78 92 'blue)
- (cons (make-ball 159 41 'green)
- empty))))
- ;; (define (lob-temp a-lob)
- ;; (cond [(empty? a-lob) ...]
- ;; [else ( ... (lob-temp (rest a-lob)) ... )]))
- ;; lob-length : LOB -> Number
- ;; Counts the number of balls in a list of Balls
- (check-expect (lob-length lob1) 3)
- (define (lob-length lob)
- (cond [(empty? lob) 0]
- [else (+ 1 (lob-length (rest lob)))]))
- ;; lob-x : LOB -> List
- ;; Extracts all x coordinates in a list of Balls
- (check-expect (lob-x lob1) (cons 50 (cons 78 (cons 159 empty))))
- (define (lob-x lob)
- (cond [(empty? lob) empty]
- [else (cons (ball-x (first lob)) (lob-x (rest lob)))]))
- ;; lob-draw : LOB -> Image
- ;; Places all balls in a list onto an empty scene
- (check-expect (lob-draw lob1) .)
- (define (lob-draw lob)
- (cond [(empty? lob) (empty-scene 300 300)]
- [else
- (place-image
- (circle 3 "solid" (ball-color (first lob)))
- (ball-x (first lob)) (ball-y (first lob))
- (lob-draw (rest lob)))]))
- ;; lob-filter : LOB -> List
- ;; Removes from the list any ball whose coordinates do not fall within 300x300
- (check-expect
- (lob-filter
- (cons (make-ball 268 156 'purple) (cons (make-ball 346 295 'pink) lob1)))
- (cons (make-ball 268 156 'purple) lob1))
- (define (lob-filter lob)
- (cond [(empty? lob) empty]
- [(or
- (> (ball-x (first lob)) 300)
- (< (ball-x (first lob)) 0)
- (> (ball-y (first lob)) 300)
- (< (ball-y (first lob)) 0)) (lob-filter (rest lob))]
- [else (cons (first lob) (lob-filter (rest lob)))]))
- ;; lob-member? : LOB Ball -> Boolean
- ;; Tells if the given Ball matches another Ball in the given LOB
- (check-expect (lob-member? lob1 (make-ball 78 92 'blue)) true)
- (check-expect (lob-member? lob1 (make-ball 78 4 'blue)) false)
- (define (lob-member? lob b)
- (cond [(empty? lob) false]
- [(and
- (= (ball-x (first lob)) (ball-x b))
- (= (ball-y (first lob)) (ball-y b))
- (symbol=? (ball-color (first lob)) (ball-color b))) true]
- [else (lob-member? (rest lob) b)]))
- ;;;;; Problem A3
- (require 2htdp/universe)
- (define-struct txt (content x y))
- ;; Txt = (make-txt String Number Number)
- ;; LoTxt is one of:
- ;; -- empty
- ;; -- (cons Txt LoTxt)
- (define orientx 200)
- (define orienty 150)
- (define txtlist
- (cons (make-txt "On your mark." orientx orienty)
- (cons (make-txt "Get set."
- orientx (+ 25 orienty))
- (cons (make-txt "Go!"
- orientx (+ 50 orienty))
- empty))))
- (define-struct world (image hidden))
- ;; World = (make-world Image LoTxt)
- ;; intepretation:
- ;; The world's image represents the image that the audience can see.
- ;; The world's list of Txt represents the yet-to-be-revealed elements.
- (define WORLD-0 (make-world (empty-scene 400 400) txtlist))
- ;; display : World -> Image
- ;; Renders current world state
- (check-expect (display WORLD-0) .)
- (define (display w)
- (world-image w))
- ;; next : World -> World
- ;; Consumes current world, produces next world with next text added
- (define (next w)
- (cond [(empty? (world-hidden w)) WORLD-0]
- [else (make-world
- (place-image (text
- (txt-content (first (world-hidden w))) 20 'blue)
- (txt-x (first (world-hidden w)))
- (txt-y (first (world-hidden w)))
- (world-image w)) (rest (world-hidden w)))]))
Add Comment
Please, Sign In to add comment