在 Common Lisp 中,在循环宏中,和 和 如 关键字似乎具有相同的功能:
(loop for i from 1 to 3 do (print i))
(loop as i from 1 to 3 do (print i))
我错过了一些细微的区别吗?如果没有,为什么要使用两个不同但相同的关键字呢?
最佳答案
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.
例如。:
(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))
为什么?!
传统! :-)
并且...
"... 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,我们编写代码供人们(包括 6 个月后的您)阅读,而不仅仅是供计算机执行。
使您的代码更具可读性的一切都是“公平游戏”。
关于common-lisp - 循环宏中的 "for"和 "as"关键字有什么区别吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41900765/