【发布时间】:2014-05-18 02:36:37
【问题描述】:
(count-by-type
(list
(make-animal "Slytherin" "snake" (make-date 2013 8 23))
(make-animal "Toby" "dog" (make-date 2014 3 20))
(make-animal "Curly" "dog" (make-date 2014 1 18))
(make-animal "Maximus" "cat" (make-date 2013 10 7))
(make-animal "Mia" "cat" (make-date 2013 10 7))))
=> (list (list 2 "cat") (list 2 "dog") (list 1 "snake"))
;; A Date is a structure (make-date y m d), where
;; y is positive integer (year),
;; m is an integer between 1 and 12 (month),
;; d is an integer between 1 and 31 (day of the month).
(define-struct date (year month day))
;; An Animal is a structure (make-structure n t a), where
;; n is a nonempty string (name of animal),
;; t is a nonempty string (type of animal),
;; a is a Date (date animal arrived at the shelter).
(define-struct animal (name type arrival))
注意:适用的数据结构如上。
所以我正在尝试制作一个函数(在使用 Dr. Racket 的 Scheme 中),它使用一个 Animal 结构列表并生成一个列表列表。
生成列表中的元素采用 (list number type) 形式,其中 type 是动物的类型,number 是消耗列表中该动物类型的编号。
这对我来说是可行的,但这就是问题所在: -> 列表需要按降序排列 -> 更糟糕的是,如果任意数量的列表对之间存在平局,则按字母排序被利用了。
注意:我希望只使用不包括 build-list 和 lambda 的抽象列表函数来解决这个问题(因为我希望加强我在没有高级抽象函数的情况下解决此类问题的能力)
记住这一点,我知道这个问题将使用抽象函数 map、filter 和 foldr,但不太清楚如何。
感谢任何帮助。
【问题讨论】:
-
我不太明白你的限制——你想使用哪些功能,你想避免哪些功能?
-
@uselpa 想要避免使用 lambda 和 build-list。
-
为什么要避免
lambda?既然你想创建一个函数,你无论如何都需要lambda。 -
@uselpa 我想先熟练使用本地定义的函数,然后再使用 lambda...
标签: list data-structures scheme