Home>

### i want to create a list using a do loop in scheme

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)))
((&gt;x 5))
(do ((y -5 (+ y 1)))
((&gt;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.

There are several possible ways, but if you assume a loop using`do`in 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))``````

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

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