方案帮助。结构列表和递归

发布于 2024-09-29 02:40:31 字数 1401 浏览 3 评论 0原文

(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) "")

我希望我解释的方式是有意义的。我已经为此苦苦挣扎了几个小时,我觉得这真的很简单,我只是对语言的理解不够。

(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))

So I have a struct for something I will call "binding" and a list which holds all the "bindings" I created. Now for the question: lets say I wanted to create a list which held the letter in each Binding that has the same number as I call a function with. For example:

;;-----------------------------------------------------------------------------
;; 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) "")

I hope this makes sense the way I explained it.. I've been struggling with this for hours and I feel like it is really simple I just don't understand the language enough.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

別甾虛僞 2024-10-06 02:40:31

类似的东西(我现在无法测试它,但这个想法应该很清楚):

(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))

这个将返回一个字母列表。您必须对它们进行排序并自己连接到字符串:)

Something like that (I cannot test it right now, but the idea should be clear):

(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))

This one will return a list of letters. You will have to sort them and join to string yourself :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文