In Common Lisp, in the loop macro, the and and as keywords appear to function identically:
(loop for i from 1 to 3 do (print i))
(loop as i from 1 to 3 do (print i))
Is there some subtle difference I'm missing? If not, why bother with two different yet identical keywords?
6.1.2.1 Iteration Control:
The
for
andas
keywords are synonyms; they can be used interchangeably. ... By convention,for
introduces new iterations andas
introduces iterations that depend on a previous iteration specification.
E.g.:
(loop for x from 1 to 10
as x2 = (* x x)
as x4 = (* x2 x2)
for y from 10 downto 1
as y2 = (* y y)
as y4 = (* y2 y2)
sum (* x4 y4))
Why?!
Tradition! :-)
And, also...
"... a computer language is not just a way of getting a computer to perform operations, but rather ... it is a novel formal medium for expressing ideas about methodology" Abelson/Sussman "Structure and Interpretation of Computer Programs".
IOW, we write code for people (including you 6 months from now) to read, not just for computers to execute.
Everything that makes your code more readable is "fair game".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With