common-lisp - 循环宏中的 "for"和 "as"关键字有什么区别吗?

标签 common-lisp

在 Common Lisp 中,在循环宏中,关键字似乎具有相同的功能:

(loop for i from 1 to 3 do (print i))
(loop as i from 1 to 3 do (print i))

我错过了一些细微的区别吗?如果没有,为什么要使用两个不同但相同的关键字呢?

最佳答案

6.1.2.1 Iteration Control :

The for and as keywords are synonyms; they can be used interchangeably. ... By convention, for introduces new iterations and as 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/

相关文章:

common-lisp - Sharpsign Colon 和 Gensym 之间的差异

lisp - 将列表中的汽车评估为 lisp 中的函数

lisp - 从 REPL 获取代码

namespaces - 在 LISP lambda 函数中使用参数作为运算符(Lisp 的根源)

common-lisp - 在 Common Lisp 中提取正则表达式匹配

common-lisp - 使用 RESTAS 和 Hunchentoot 进行用户身份验证

apache - 使用 Common Lisp Apache fastcgi

lambda - 如何在 LISP 中定义 LAMBDA 函数?

lisp - scbl 异常堆在垃圾收集期间耗尽

scheme - Common Lisp和Scheme之间的通用词汇