How to update list of structs inside another struct in Racket (Universe)

234 Views Asked by At

This is a homework assignment so I am not asking for code just guidance. I am new to Racket language and using ISL without lambda. I can't use lambda or any other libraries.

I am using bigbang. I have a struct called struct-abc. Inside struct-abc, I have a list called list-abc. Inside list-abc, I have a collection of struct-xyz. I need to update all the struct-xyz inside list-abc that is inside struct-abc on every tick.

Here is what I have so far:

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

With the above code snippet, I am able to add new instances of struct-xyz to list-abc on mouse-event or key-event. Now how do I update all the struct-xyz in list-abc automatically on every tick?

Here is my bigbang function (not including all parameters):

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

Here is my world-after-tick function where I pass parameters world and list-abc to a helper function:

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

Now, in this helper function, I want to pass the first struct-xyz from list-abc to another helper function that updates the values and recursively pass every struct from list-abc. How do I call the recursive function as well as the function that updates the value? I am not able to figure this part out:

(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

There are 1 best solutions below

0
On

The names catch me off guard, i would recomend breaking down your main function into levels. Each level does a different thing with the information provided to it. You decide which level does what based off what each task needs access to. Like molbdnilo said map would solve a big section of your problem.