So, I can loop with one variable like this:
(loop for i in ‘(1 2 3 4 5) do (print i))
I can get 1 number and bind it to i.
But how can I bind to more than 1 variable at once, e.g. something like for i,j,k in '(1 2 3 4 5) -> i = 1, j = 2, k = 3
I already tried searching around but only found with statement to define more variables, but it does not allow me to bind the variables directly.
>Solution :
You can bind multiple variables simultaneously in a loop by using a sublist for each variable. Here’s an example of how you can achieve it:
(loop for (i j k) in '((1 2 3) (4 5 6) (7 8 9))
do (print (list i j k)))
The loop iterates over a list of lists, and each inner list contains three elements. The variables (i, j, k) get bound to the elements of each inner list. Then, within the loop body, you can perform any desired operations using these variables.
When executed, the above code will print:
(1 2 3)
(4 5 6)
(7 8 9)
EDIT: The OP is looking for a result that involves only one list. This is also achievable in Common Lisp:
(loop for (i j k) on '(1 2 3 4 5 6 7 8 9)
by #'cddr
while k
do (print (list i j k)))