gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 03:27:15 24 4
gpt4 key购买 nike

在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com