(defun prefix (a y) (cond ((null y) nil)
(t (cons (cons a (car y)) (prefix a (cdr y))))))
(setq result prefix(a (cons 1 2)))
(print result)
这个函数 cdr 遍历列表 y,递归地打印 (a (car y))。如果 P 是第一个参数,y 是列表 (1 2 3),它应该返回 ((P1) (P2) (P3))。但是,当我尝试为其提供参数并执行它时,我无法使该功能正常工作。这里有什么不正确的地方?
最佳答案
This function cdr's through the list y, printing (a (car y)) recursively.
它如何打印任何东西?函数中没有一条打印语句。
If P is the first parameter, and y is the list (1 2 3), it should return ((P1) (P2) (P3)).
通过查看您的代码,它将返回 ((P . 1) (P . 2) (P . 3))
而不是 ((P1) (P2) (P3 ))
。
However, I can't get the function to work when I try to give it parameters and execute it. What is incorrect here?
你的电话看起来不对。 prefix( ... )
有些东西看起来不像 Lisp。在 Lisp 中调用函数的语法是 (prefix ... )
。函数名称位于左括号之后。
(defun prefix (a y)
(cond ((null y) nil)
(t (cons (cons a (car y))
(prefix a (cdr y))))))
(setq result prefix(a (cons 1 2))) ; this line has problems
(print result)
关于lisp - 我的 Lisp 代码无法运行,但函数本身可以编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13775831/