Question: ( @htdf find - path ) ( @signature String String Map - > ( listof String ) or false ) ;; try to find path

(@htdf find-path)
(@signature String String Map ->(listof String) or false)
;; try to find path between from and to, while enforcing door lock rules
;(define (find-path start to castle) false) ;stub
(define (find-path start to castle)
;; path is (listof String), the path from start to the current room
;; key-gained is (listof Natural), the keys that gain so far
(local [(define (fn-for-room r path key-gained)
(cond [(string=? to (room-name r) to)(append path (list to))]
[(member?(room-name r) path) false]
[else
(fn-for-los (room-exits r)
(append path (list (room-name r)))
(append (room-keys r) key-gained))]))
(define (fn-for-los los path key-gained)
(cond [(empty? los) false]
[else
(local [(define approved-los (real-exits los key-gained))
(define try (fn-for-room
(generate-room
(first approved-los) castle)
path
key-gained))]
(if (not (false? try))
try
(fn-for-los (rest approved-los)
path key-gained)))]))
(define (real-exits rooms keys)
(local [(define (fn? r)
(all-in?(room-locks (generate-room r castle)) keys))]
(filter fn? rooms)))
(define (all-in? locs keys)
(andmap (\lambda (n)(member? n keys)) locs))]
(if (not (empty?(room-locks (generate-room start castle)))) false
(fn-for-room (generate-room start castle) empty
empty))))
(@problem 2)
;;
;; Convert the function above to use tail recursion. Call this new function
;; find-path/tr, so that the grader can test both versions.
;;
;; HINT: You will need 3 worklists working together (in tandem).
;;
(@htdf find-path/tr)
(@signature String String Map ->(listof String) or false)
;; try to find path between from and to, while enforcing door lock rules
(check-expect (find-path/tr "A""B" MAP) false)
(check-expect (find-path/tr "A""D" MAP) false)
(check-expect (find-path/tr "C""E" MAP) false)
;(define (find-path/tr start to castle) false) ;stub

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!