(defun func (in s f l)
(cond
((null in) (append l (list (list 'end (+ 1 f)))))
((eq (car in) 'foo) (foo-asd (cdr in) s f l))
((atom (car in))(atom-asd in (+ 1 s) (+ 1 f) l))
))
.
(defun atom-asd (in s f l)
(cond ((eql in nil) ())
(append l (list (list 'frob s (car in) (+ 1 f))))))
.
(defun foo-asd (in s f l)
(cond
((eql in nil) (append l (list (list 'frob s 'myst f))))
((func in s f (append l (list (list 'frob s 'myst (+ 1 f))))))
((foo-asd (cdr in) s f l))
))
.
关于这段代码,如果调用(func '(foo x y) 0 0 ())
函数foo-asd
将被调用,那么func
是再次调用会进入函数atom-asd
,当atom-asd
执行结束,所有程序结束,不调用递归调用foo-asd
。我需要调用 foo-asd
,但我不明白为什么在 atom-asd
结束后它没有被调用。
最佳答案
[4]> (trace func)
;; Tracing function func.
(func)
[5]> (trace atom-asd)
;; Tracing function atom-asd.
(atom-asd)
[6]> (trace foo-asd)
;; Tracing function foo-asd.
(foo-asd)
[7]> (func '(foo x y) 0 0 ())
1. Trace: (func '(foo x y) '0 '0 'nil)
2. Trace: (foo-asd '(x y) '0 '0 'nil)
3. Trace: (func '(x y) '0 '0 '((frob 0 myst 1)))
4. Trace: (atom-asd '(x y) '1 '1 '((frob 0 myst 1)))
*** - cond: variable append has no value
The following restarts are available:
USE-VALUE :R1 Input a value to be used instead of append.
STORE-VALUE :R2 Input a new value for append.
ABORT :R3 Abort main loop
Break 1 [8]>
所以在atom-asd
中:
(defun atom-asd (in s f l)
(cond
((eql in nil)
())
(append
l (list (list 'frob s (car in) (+ 1 f))))))
您有两个谓词。一个测试 (eql in nil)
显然是 nil
,然后它检查变量 append
是否有一个非 nil
值(value)。问题是 append
不是绑定(bind)变量。它是函数命名空间中的绑定(bind),但这里每个术语都包含在一组括号中,因此 append
本身就是要测试的表达式。你可能意味着它应该在第一个学期没有开始时执行 (append ...)
并且你应该这样写:
(defun atom-asd (in s f l)
(cond
((eql in nil)
())
(t
(append l (list (list 'frob s (car in) (+ 1 f)))))))
在这个版本中我们得到了一个结果:
[8]> (func '(foo x y) 0 0 ())
5. Trace: (func '(foo x y) '0 '0 'nil)
6. Trace: (foo-asd '(x y) '0 '0 'nil)
7. Trace: (func '(x y) '0 '0 '((frob 0 myst 1)))
8. Trace: (atom-asd '(x y) '1 '1 '((frob 0 myst 1)))
8. Trace: atom-asd ==> ((frob 0 myst 1) (frob 1 x 2))
7. Trace: func ==> ((frob 0 myst 1) (frob 1 x 2))
6. Trace: foo-asd ==> ((frob 0 myst 1) (frob 1 x 2))
5. Trace: func ==> ((frob 0 myst 1) (frob 1 x 2))
((frob 0 myst 1) (frob 1 x 2))
关于lisp - 从未调用过 Lisp 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57976010/