Question: Hi i need help getting these problems solved by using accumulators in DrRacket. Details below before the problems are the data definitions which will be
Hi i need help getting these problems solved by using accumulators in DrRacket. Details below before the problems are the data definitions which will be used to solve the problems.
(@htdd Status) ;; Status is one of: ;; - "buried" ;; - "sunken" ;; - "locked" ;; interp. the status of an unopened treasure box ;;
(@htdd Treasure) (define-struct treasure (label amount difficulty status routes)) ;; Treasure is (make-treasure String Natural Natural Status (listof Route)) ;; interp. a treasure box with a label name, ;; the number of gold coins contained in the treasure box, ;; a rating of difficulty to find and open the treasure box between 1 ;; and 5, where 1 is very easy to find and open and 5 is very difficult, ;; the status of the treasure box before it was opened, ;; and a list of routes leading from this treasure box ;; to other treasure boxes
(@htdd Route) (define-struct route (duration destination)) ;; Route is (make-route Natural String) ;; interp. a route leading from one treasure box to another ;; duration is the time in hours it will take to travel to it and ;; destination is the name of the treasure box the route leads to
(define TREASURE-MAP (list (make-treasure "E" 32 3 "buried" (list (make-route 3 "A"))) (make-treasure "F" 10 2 "locked" (list (make-route 7 "C"))) (make-treasure "B" 6 5 "locked" (list (make-route 9 "E") (make-route 15 "F"))) (make-treasure "J" 1 1 "sunken" (list (make-route 6 "I"))) (make-treasure "H" 17 2 "sunken" (list (make-route 15 "J") (make-route 4 "I"))) (make-treasure "G" 52 3 "buried" (list (make-route 2 "D"))) (make-treasure "I" 100 5 "locked" empty) (make-treasure "D" 21 1 "sunken" (list (make-route 8 "G") (make-route 13 "H") (make-route 9 "I") (make-route 11 "A"))) (make-treasure "C" 41 4 "buried" (list (make-route 6 "G"))) (make-treasure "A" 7 1 "locked" (list (make-route 12 "B") (make-route 7 "C") (make-route 27 "D")))))
;; Consider this to be a primitive function that comes with the data definitions ;; and that given a treasure name it produces the corresponding treasure. ;; Because this consumes a string and generates a treasure calling it will ;; amount to a generative step in a recursion through a graph of treasures and ;; routes. You must not edit this function, but you can experiment with it to ;; see how it works.
;;(@htdf lookup-treasure) ;;(@signature String -> Treasure) (define (lookup-treasure name) (local [(define (scan lst) (cond [(empty? lst) (error "No treasure named " name)] [else (if (string=? (treasure-label (first lst)) name) (first lst) (scan (rest lst)))]))] (scan TREASURE-MAP)))
(define TE (lookup-treasure "E")) (define TF (lookup-treasure "F")) (define TB (lookup-treasure "B")) (define TJ (lookup-treasure "J")) (define TH (lookup-treasure "H")) (define TG (lookup-treasure "G")) (define TI (lookup-treasure "I")) (define TD (lookup-treasure "D")) (define TC (lookup-treasure "C")) (define TA (lookup-treasure "A"))
(define (fn-for-treasure t) (local [(define (fn-for-status s) (cond [(string=? s "buried") (...)] [(string=? s "sunken") (...)] [(string=? s "locked") (...)]))
(define (fn-for-treasure t) (... (treasure-label t) (treasure-amount t) (treasure-difficulty t) (fn-for-status (treasure-status t)) (fn-for-lor (treasure-routes t))))
(define (fn-for-lor lor) (cond [(empty? lor) (...)] [else (... (fn-for-route (first lor)) (fn-for-lor (rest lor)))]))
(define (fn-for-route r) (... (route-duration r) ;; lookup-treasure is the generative step that makes the whole ;; MR generative (fn-for-treasure (lookup-treasure (route-destination r)))))]
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;(@problem 1) ;; Design a function that consumes a treasure and produces the total amount ;; of gold that can be obtained by opening that treasure, and all treasures ;; reachable from that treasure. ;; ;; Your solution MUST be tail recursive. ;;
(@htdf reachable-gold) uncomment this when you begin problem 1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(@problem 2) ;; ;; Complete the function that lists the label names of all reachable treasure. ;; ;; Your solution MUST be tail recursive. ;;
(@htdf all-labels) (@signature Treasure -> (listof String)) ;; produce the label names of all reachable treasures (check-expect (all-labels TI) (list "I")) (check-expect (all-labels TH) (list "H" "J" "I")) (check-expect (all-labels TA) (list "A" "B" "E" "F" "C" "G" "D" "H" "J" "I"))
;(define (all-labels t) empty) ;stub
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
