介紹
Instructions:
對于這一點,所有后續的分配,預計將使用設計配方你寫的功能,包括輔助函數。從分配的描述,不要直接復制的目的。其目的應寫在你自己的話,包括參考的參數名稱您的功能。
• For this and all subsequent assignments, you are expected to use the design recipe for
the functions you write, including helper functions.
• Do not copy the purpose directly from the assignment description. The purpose
should be written in your own words and include reference to the parameter names of
your functions.
• The solutions you submit must be entirely your own work. Do not look up either full
or partial solutions on the Internet or in printed sources.
• Do not send any code files by email to your instructors or tutors. It will not be
accepted by course staff as an assignment submission. Course staff will not debug
code emailed to them.
• Test data for all questions will always meet the stated assumptions for consumed
values.
organize and submit your work.
• Download the interface file from the course Web page.
• Follow the instructions in the style guide. Specifically, your solutions should be
placed in files a4qY.rkt, where Y is a value from 1 to 4.
• For full marks, it is not sufficient to have a correct program. Be sure to follow all the
steps of the design recipe, including the definition of constants and helper functions
where appropriate.
• Read each question carefully for restrictions.
•如果一個問題需要生成遞歸使用的,必須有一個生成遞歸元素到您的解決方案。遞歸結構,累計也可以使用遞歸,和抽象的列表功能。
•你創建的所有輔助功能應該被定義在本地的主要 函數定義或使用lambda。
•使用Lambda函數,一次性使用的功能。
• If a question requires the use of generative recursion, there must be a generative
recursive element to your solution. Structural recursion, accumulative
recursion, and abstract list functions may also be used.
• All helper functions you create should be defined locally within the main#p#分頁標題#e#
function definition or use lambda.
• Use lambda for small, single-use functions.
Language level: Intermediate Student with lambda.
Coverage: Module 4
Useful structure and data definitions:
In this assignment, use the following structure and data definitions. Include these
definitions in your solution files.
(define-struct clue (current points next))
;; A clue is a structure
;; (make-clue cur pts nxt), that represents
;; a clue, where
;; cur is a lowercase string representing
;; the location of this current clue
;; pts is a natural number representing a number of points
;; nxt is a lowercase string representing
;; the location of the next clue
;; A t-map is a list of clue structures representing
;; paths between locations. Two locations A and B are
;; linked in t-map when there is a clue in the list that
has current as A and its next as B. No clue has the
;;
;; same value for current and next. No two clues
;; have the same current location and the same number of points.
;; A t-map can describe multiple paths. No path
;; has a loop in it. A loop would occur
;; if a location is encountered
;; twice along a path in t-map.
Example 1:
(list
(make-clue “hay loft” 10 “shed”)
(make-clue “shed” 20 “pool”)
(make-clue “shed” 30 “apple tree”))
(make-clue “barn” 20 “shed”))
contains four paths:
- “hay loft” >> “shed” >> “pool”
- “hay loft” >> “shed” >> “apple tree”
- “barn” >> “shed” >> “pool”
- “barn” >> “shed” >> “apple tree”
Notice that none of these paths contain a loop (no location is encountered twice along a
path). Because there is no loop in a path, every path terminates at a point where it
reaches a location that is not the current location of any clue in the list.
Example 2:
(list
(make-clue “hay loft” 10 “shed”)
(make-clue “shed” 20 “pool”)
(make-clue “pool” 10 “hay loft”))
(group-by-location
(list
(make-clue “hay loft” 10 “shed”)
(make-clue “shed” 20 “pool”)
(make-clue “apple tree” 30 “hay loft”)
(make-clue “hay loft” 50 “front steps”)
(make-clue “apple tree” 20 “tree house”))
=>
(list
(make-clue “hay loft” 10 “shed”)
(make-clue “hay loft” 50 “front steps”)
(make-clue “shed” 20 “pool”)
(make-clue “apple tree” 30 “hay loft”)
(make-clue “apple tree” 20 “tree house”))
contains a path with a loop in it: “hay loft” >> “shed” >> “pool” >> “hay loft”. This list
is NOT a t-map.
1. Write a function, sort-tm, that consumes a t-map and produces a t-map with a list
of the clues in the consumed t-map sorted alphabetically (increasing) by current
location of the clue. If two clues are in the same current location, they should be
ordered from highest to lowest points. Use the built-in quicksort function to
implement sort-tm. Do not use any explicit recursion in your solution.
2. Use generative recursion to write a function, group-by-location, that consumes
a t-map and produces a t-map that has the clues grouped by current location (all clues
with the same current location should appear together in the list). These groups
should be ordered in the order that a current location first appears in the consumed
list. Within a group, the clues should be ordered in the order they appear in the
consumed list. This order is not necessarily alphabetical and therefore the result
produced by this function may not be the same as the result produced by the function
of Question 1. For example,