Comp210
Due: 97.Mar.25 (Wed.) or Mar.27 (Fri.) in class
Recall that make-lists-fun (seen in lecture and similar to lab 8's fold) is the last list-processing function you'll ever need:
;; make-lists-fun: ;; Based on the generalized template for lists, ;; where you pass in the base-case answer and ;; the rule to combine the first element with the ;; result of the recursive call, ;; and you are given back the corresponding list-processing-function. ;; ;; If you are wondering, the contract of the function which is returned is ;; answer-fun: list-of-alpha --> beta ;; and so therefore we must have the contract ;; make-lists-fun: beta, (alpha, beta --> beta) ;; --> (list-of-alpha --> beta) ;; (define make-lists-fun (lambda (base combine) (local [(define answer-fun (lambda (lyst) (if (null? lyst) base (combine (first lyst) (answer-fun (rest lyst))))))] answer-fun)))We can now use make-lists-fun to create any function which follow the template for lists, e.g.
(define sum-numbers (make-lists-fun 0 +))
total-inventory
is a list
of these inventory records,
representing all items for sale.
Write an example of this.
total-inventory
for items of that color.
(What previous function is the most appropriate for this?)
total-inventory
is still
the same list, but check to ensure that the correct elements
in the list have been modified (mutated).
As the player walks through the maze, she can collect two items. When the player enters a cave, she may pick up the item, if one of the pockets is empty or exchange one of the two items for the item in the cave. Once the player is happy with the state of the items, she moves on by choosing one of the exits out of the cave. The game is over when the player exits the maze.
Task 2 Define the function create-player, which accepts a cave and creates a player who is in that cave and with nothing (null) in their pockets. You will also need the functions player-left-pocket, player-right-pocket, and player-current-cave. Then define the function swap-items, which accepts a player, a cave (if you like), and one of the symbols'left
, 'right
.
It swaps the item (or null
) in the indicated
pocket with the item in the cave.
Swap-items has no (useful) return value.
The walk is simulated with a simple generative recursive program. It prints a question and reads a typed response from the keyboard. The response is one of a pre-determined number of symbols: 'show-pockets, 'show-exits, 'swap-left, 'swap-right and 'leave. In the latter case, the program asks one more question which must be answered with a number. It then selects the exit from the current cave with this number and moves the player to this next cave.
Task 3 Implement the dialogue. You may use the following functions:
Here is a sample dialogue:
You entered the cave LovettHall. It contains water. What would you like to do next? swap-left You just put down shoes and picked up water. What would you like to do next? swap-right You just put down bread and picked up shoes. What would you like to do next? show-pockets You own water in the left and shoes in the right pocket. What would you like to do next? leave There are 5 exits. Which one would you like to take? 3 ...(A better dialogue might list the names of the five exits.)
(printf "You entered the cave ~s. It contains ~s.~n" 'LovettHall 'water)
End of Problem This problem is open-ended and flexible. The design of the dialogue is up to you. You are in control of how you output descriptions of items, caves, or connections, if you choose to do so. It may be a simple text-based dialogue as above. It is acceptable to implement the minimum described above, but don't feel constrained by this standard; if you implement more, tell us. Be creative!