(define-struct binding
(
let ; a string
num ; a number
)
)
(define Bind-A (make-binding empty 1))
(define Bind-B (make-binding "A" 2))
(define Bind-C (make-binding "F" 1))
(define Bind-D (make-binding "A" 1))
(define Bind-E (make-binding "C" 1))
(define Bind-F (make-binding "E" 3))
(define Bind-All (list Bind-A Bind-B Bind-C Bind-D Bind-E Bind-F))
所以我有一个结构用于我称之为“绑定”的东西,以及一个包含我创建的所有“绑定”的列表。现在提出这个问题:假设我想创建一个列表,其中包含每个Binding中的字母,该列表与我调用函数的编号相同。例如:
;;-----------------------------------------------------------------------------
;; Return a string containing the letters (in alphabetical order, separated by a
;; space) of all bindings with the same number in pool of "bindings".
;; If either letter is unknown or if no bindings have the same number
;; Return the null string ("").
;;-----------------------------------------------------------------------------
(define (same-num ; string
which-binding) ; binding to check
pool ; list of all bindings
)
(cond
[(empty? (binding-let which-binding)) ""]
[(equal? (binding-let which-binding) (binding-let (first pool)) ... ]
[else ... ]
)
)
(check-expect (same-num Bind-E Bind-all) "A F")
(check-expect (same-num Bind-F Bind-all) "")
(check-expect (same-num Bind-A Bind-all) "")
我希望这对我解释它的方式是有道理的。我一直在努力解决这个问题几个小时,我觉得这很简单,我只是不懂语言。
答案 0 :(得分:1)
类似的东西(我现在无法测试,但想法应该清楚):
(define (same-num which-binding pool)
(define (iter which lst result)
(cond
((null? (binding-let which-binding)) result)
((null? lst) result)
((equal? (binding-let which-binding)
(binding-let (car lst)))
(iter which (cdr lst) (cons (binding-let (car lst)) result)))
(else (iter which (cdr lst) result))))
(iter which-binding pool null))
这个将返回一个字母列表。你必须对它们进行排序并加入自己的字符串:)