Why does this code not print out the content of the array –
(defun loopfn (state)
(loop for x from 0 to 2 do
(loop for y from 0 to 2 do
(aref state x y))))
Here I am passing a 3×3 matrix which is built like this –
`(setq i (make-array '(3,3) :initial-contents '((0 1 3) (4 2 5) (7 8 6))))`
I am calling – (loopfn i)
Edit——–
@Greg
Thanks for pointing that out…
I had the following question..
Why does this print the output …
(defun loopfn ()
(loop for x from 0 to 3 do
(if (eq x 2)(return (list x)))))
Where as this prints a nil…
(defun loopfn ()
(loop for x from 0 to 2 do
(loop for y from 0 to 2 do
(if (eq x 2)(return (list x y))))))
I am calling
(loopfn)
Your call to
arefis getting the specified element, but you’re not doing anything with it. You could stick it on to a list which is then returned:or you could just print it out:
The former is far more useful ; you want to return things which can then be further processed, and anything that gets passed to the top level will be printed out for you.
As long as you are using
LOOPyou can easily gather up your values withCOLLECT,APPEND, etc., which is the idiomatic way to do it.