Home>
I want to make a list of vector3 using do loop in

scheme.
The do loop part was managed somehow.

(do ((x -5 (+ x 1)))
     ((>x 5))
   (do ((y -5 (+ y 1)))
       ((>y 5))
     (print x "" y "\ n")))


Here, using this loop, I would like to make a list with vector3 as an element containing elements (x, y, 0) in the order that this loop runs.

Description of vector3

I don't know what to do.
If there is a person who understands, please.

  • Answer # 1

    There are several possible ways, but if you assume a loop usingdoin that way, it is best to assemble the list from the end and invert it at the end I think this is an easy way.

    (let ((r '()))
      (do ((x -5 (+ x 1)))
          ((>x 5))
        (do ((y -5 (+ y 1)))
            ((>y 5))
          (set! r (cons (vector3 x y 0) r))))
      (reverse r))

  • Answer # 2

    If you don't need to use do, it looks like this.

    (map (lambda (x) (map (cut vector3 x<>0) (iota 11 -5)))
         (iota 11 -5))

  • Answer # 3

    There is a way to add more to the end of the linear list. (・ ∀ ・)

    (define my-vector3-list
      (let * ((queue (list #f)) (queue_last queue));queue_last is the last cell in the linear list.
        (do ((x -5 (+ x 1))) ((>x 5))
          (do ((y -5 (+ y 1))) ((>y 5))
            (set-cdr! queue_last (list (vector3 x y 0)))
            (set! queue_last (cdr queue_last))
          )
        )
        (cdr queue);Since the first #f is garbage data, it is discarded
      )
    )