0

私は 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 を関数として評価しようとしている理由がわかりません。

4

1 に答える 1

3

問題は、isinlistが として再帰的に呼び出されること(isinlist(x (cdr y)))です。 (x ...)は function の関数呼び出しとして解釈されますx(isinlist x (cdr y))おそらく代わりに欲しいでしょう。

ちなみに、 (with isinlist)に置き換えることもできます。member:key #'cdr :test #'equal

于 2014-12-11T22:34:53.133 に答える