lisp - 我的 Lisp 代码无法运行,但函数本身可以编译

标签 lisp common-lisp

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

相关文章:

object - Lisp:基本的面向对象计数器

python - 在有越位规则的语言中,所有语言结构都可以是一流的吗?

common-lisp - Common Lisp SBCL 循环性能调整

performance - 用于性能的 defclass 类型信息

common-lisp - def参数值不会自动更改

lisp - 在 Common Lisp 和 Let 中修改哈希表

emacs - 使用 plt-r5rs 而不是 Racket 调用 Geiser?

lisp - 尾调用和尾递归有什么区别?

lisp - Lisp 中的未绑定(bind)变量错误

type-conversion - 在Common Lisp中将整数转换为字符