如何更新Racket(Universe)中另一个结构内的结构列表

时间:2017-10-07 18:19:00

标签: list racket racket-student-languages

这是一项家庭作业,所以我不是要求代码只是指导。 我是Racket语言的新手,并且使用没有lambda的ISL。 我不能使用lambda或任何其他库。

我正在使用bigbang。我有一个名为struct-abc的结构。在struct-abc里面,我有一个名为list-abc的列表。在list-abc里面,我有一个struct-xyz的集合。我需要在每个tick上更新struct-abc里面的list-abc里面的所有struct-xyz。

这是我到目前为止所做的:

; my main struct - struct-abc

    (define-struct struct-abc (list-abc))

; my struct - struct-xyz

    (define-struct struct-xyz (pos1 pos2 pos3 radius))

; my list - list-abc which adds instances of struct-xyz to the list

    (define (list-abc struct-xyz-instance lst)
      (cond
       [(empty? lst) (cons struct-xyz-instance empty)]
       [else (cons struct-xyz-instance lst)]))

使用上面的代码片段,我可以在mouse-event或key-event上将list-xyz的新实例添加到list-abc。现在如何在每个tick上自动更新list-abc中的所有struct-xyz?

这是我的bigbang函数(不包括所有参数):

(define (main rate)
  (big-bang (initial-world rate)
            (on-tick world-after-tick rate)
            (on-draw ...)))

这是我的world-after-tick函数,我将参数world和list-abc传递给辅助函数:

(define (world-after-tick-recursive world)
    (update-world-recursive world (world-list-abc world)))

现在,在这个辅助函数中,我想将第一个struct-xyz从list-abc传递给另一个帮助函数,该函数更新值并递归传递list-abc中的每个结构。如何调用递归函数以及更新值的函数?我无法弄清楚这一部分:

(define (update-world-recursive world abc-list abc-rest-list)
  (cond
   [(empty? abc-list) #false] ; If empty end bigbang
   [else
     (update-world world (first abc-list) (rest abc-list))]
; Where and how do I call the update-world-recursive function?


(define (update-world world abc-list) ; This takes the first struct from list
... update pos1 of abc-list
... update pos2 of abc-list)

1 个答案:

答案 0 :(得分:0)

这些名字让我措手不及,我建议将你的主要功能分解为等级。每个级别对提供给它的信息都有不同的作用。您可以根据每个任务需要访问的内容来确定哪个级别的功能。像molbdnilo说地图可以解决你问题的很大一部分。