我需要这样做,但在ACL2中:
for (i=1; i<10; i++) {
print i;
}它使用普通的LISP,但我不知道如何完成这个任务.
我们不能使用标准的通用Lisp结构,比如循环,DO。只是递归。
我有一些联系,但我发现很难理解:
发布于 2011-01-21 13:53:34
ACL2编程入门中的“访问从n到0的所有自然数”一节解释了如何做到这一点。
在您的示例中,您希望按升序访问数字,因此代码应该如下所示:
(defun visit (n max ...)
(cond ((> n max) ...) ; N exceeds MAX: nothing to do.
(t . ; N less than or equal to MAX:
. n ; do something with N, and
.
(visit (+ n 1) max ...) ; visit the numbers above it.
.
.
.)))发布于 2011-01-21 03:55:26
使用递归的解决方案:
> (defun for-loop (from to fn)
(if (<= from to)
(progn
(funcall fn from)
(for-loop (+ from 1) to fn))))
;; Test
> (for-loop 1 10 #'(lambda (i) (format t "~a~%" i)))
1
2
3
4
5
6
7
8
9
10
NIL发布于 2014-07-21 04:09:47
(n) (cond ((zp n)“x0”) (t (级数2$ (cw "~x0“n) (foo-循环(1-n)
(foo-循环10)
您可以重做终止条件和递归,以模拟从1到10。
https://stackoverflow.com/questions/4753630
复制相似问题