The loop facility in Common Lisp allows several value accumulation clauses, maximize amongst others.
Now, it is also possible to give a variable var to the maximize clause:
(loop for x from 0 to 10 maximize (func x) into var)
My question is:
Is it possible to give as var a new local variable introduced by let?
An example scenario would be:
(let ((var -1)) ; assume numeric result
(loop for x from 0 to 10 maximize (func x) into var))
It is not important that x has a numeric value, it's only for illustration purposes.
Mix bindings?
No, the
intovariables are bound byloop.What you can do is bind your
varto the return value ofloop:Complex loop - use multiple values, functional style
If you are doing many things in a single loop, you might want to use values function in Common Lisp:
Note that the variables
max,minandsumbound bymultiple-value-bindandloopare completely separate and independent, and have absolutely nothing in common and are named the same for didactic purposes only.If you rename them (as you definitely should for the sake of code readability!):
and recompile your code, you will see that the disassembly is identical.
Complex loop, use
finally, procedural styleAs suggested by @coredump, you can set your variables in the
finallyconstruct:Generally, speaking, there is more than one way to skin the cat here...