【问题标题】:Undefined function in lisplisp 中未定义的函数
【发布时间】:2014-12-11 21:21:25
【问题描述】:

我正在编写一个 lisp 函数,当我尝试从函数返回 x 的值时,我不断收到 EVAL - undefined function x。

(defun p+ (x y)
    (recurcollect (gluelist x y) '()))

(defun isinlist (x y)
    (if (car y)
        (if (equal (cdr x) (cdar y))
            t
            (if (cdr y)
                (isinlist(x (cdr y))) 
                NIL))
        NIL))

(defun collectvalue (x y) ;takes an expression and a list and returns the sum of all like expressions from the list
    (if (equal x NIL)
        (print '(x is NIL))
        (if (equal (cdr x) (cdar y)) 
             (if (cdr y)
                (collectvalue (list (+ (car x) (caar y)) (cdr x)) (cdr y))
                (list (+ (car x) (caar y)) (cdr x)))
        (if (cdr y)
            (collectvalue x (cdr y))
            x))))

(defun recurcollect (x y) ;returns a flat list of collected expressions
    (if (isinlist (car x) y)
        (recurcollect (cdr x) y)
        (if (cdr x)
            (recurcollect x (cons y (collectvalue (car x) (cdr x))))
            (cons y (car x)))))

(defun gluelist (x y)
    (if (cdr x)
        (cons (car x) (gluelist (cdr x) y))
        (cons (car x) y)))

(print (p+ '(2 0 1) '(4 0 1)))  ;(6 0 1)

我相信错误是由函数末尾的 x 引起的,但我不明白为什么,据我所知,我的括号正确配对,我不明白为什么它试图将 x 作为函数进行评估.

【问题讨论】:

  • 您是否在尝试定义函数或尝试运行它时遇到此问题?你在 REPL 上具体评估了什么?
  • @L33tminion 它发生在我运行它时。我正在评估的是多项式,每个值都表示为(系数、第一个变量的幂、第二个变量的幂等)所以 3x^2 将是 (3 2) 3xy^2 将是 (3 1 2) 所以在。 x 是单个项,y 是表示多项式的项列表。此功能的目的是收集所有类似的术语。
  • 请正确格式化您的代码(emacs 会为您完成)并复制并粘贴您的完整交互,即您如何调用您的函数以及 Lisp 打印的内容。
  • @sds 我使用的是公用计算机,因此无法访问 emacs,您能告诉我格式的哪些区域需要修复吗?
  • 您在 REPL 上到底在做什么?你能复制并粘贴确切的东西吗?

标签: lisp common-lisp undefined-function


【解决方案1】:

问题在于isinlist,它被递归调用为(isinlist(x (cdr y)))。 (x ...) 被解释为函数x 的函数调用。 你可能想要(isinlist x (cdr y))。

顺便说一句,您可以将isinlist 替换为member(使用:key #'cdr :test #'equal)。

【讨论】:

  • 我认为您可能是对的,但是由于输入的性质,成员不会起作用,它只需要测试 cdr 的相等性。
  • 我添加了建议的member关键字参数
猜你喜欢
  • 2021-12-26
  • 2016-08-21
  • 2013-12-08
  • 1970-01-01
  • 1970-01-01
  • 2023-03-07
  • 2022-11-16
  • 2016-03-05
  • 1970-01-01
相关资源
最近更新 更多