mpg*_*pgn 0 lisp syntax common-lisp do-loops
在lisp中我有一点问题需要理解
我有这个代码:
(defun iota-b (n)
(do ((x 0 (+1 x))
(u '() (cons x u)))
((> x n) (nreverse u))))
Run Code Online (Sandbox Code Playgroud)
(iota-b 5)
(0 1 2 3 4 5)
在文档中,"do"基本模板是:
(do (variable-definitions*)
(end-test-form result-form*)
statement*)
Run Code Online (Sandbox Code Playgroud)
我真的不明白我的身体在我的身体中的位置iota-b对我而言
(你'()(cons xu)))
显然不是,为什么我们把(u'()(cons xu)))放在变量定义中?
您有表单的变量定义 var init [step]
((x 0 (+1 x))
(u '() (cons x u)))
Run Code Online (Sandbox Code Playgroud)
这增加x
在每次迭代中,并与建立(cons x u)
的u
列表作为(5 4 3 2 1 0)
.
最终测试
(> x n)
Run Code Online (Sandbox Code Playgroud)
结果表格
(nreverse u)
Run Code Online (Sandbox Code Playgroud)
将列表反转为(5 4 3 2 1 0)
给定结果.
然后你有一个空体.
您当然可以将do循环修改为
(do ((x 0 (+1 x))
(u '()))
((> x n) (nreverse u))
(setq u (cons x u)))
Run Code Online (Sandbox Code Playgroud)
这将得到相同的结果.