LISP“错误非法函数对象...”; [英] LISP "Error Illegal function object..."

查看:87
本文介绍了LISP“错误非法函数对象...”;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始在课堂上学习Lisp。我正在做一项作业,其中我必须使用一些基本操作来编程一些非常基本的功能,例如car,cdr,cons,append,append,reverse,atom,eq,equal和null。

I just started learning lisp in my class. I'm doing a homework assignment in which I have to program a few very basic functions using some primitives operations such as car, cdr, cons, append, reverse, atom, eq, equal, and null.

所以这是我到目前为止的代码:

So here is my code so far:

(defun union2 (l1 l2)
  (cond ((null l1) l2)
        ((member((car l1) l2)) (union2((cdr l1) l2)))
        (t (cons (car l1) (union2((cdr l1) l2)))))
  )

当我尝试运行时我的测试:

When I try to run my test:

(union2 '(5 7 2 3 1) '(3 2 4 6 9))

我得到一个错误:非法函数对象:(汽车l1)。我的印象是我正确编写了代码。我究竟做错了什么?
谢谢您的时间。

I get an "Error: Illegal function object: (car l1)." I was under the impression that I was writing the code correctly. What am I doing wrong? Thank you for your time.

推荐答案

尝试使用编译器,例如SBCL:

Try using a compiler, like SBCL:

* (defun union2 (l1 l2)
  (cond ((null l1) l2)
        ((member((car l1) l2)) (union2((cdr l1) l2)))
        (t (cons (car l1) (union2((cdr l1) l2)))))
  )
; in: DEFUN UNION2
;     ((CAR L1) L2)
; 
; caught ERROR:
;   illegal function call

;     (MEMBER ((CAR L1) L2))
; 
; caught WARNING:
;   The function was called with one argument, but wants at least two.

;     ((CDR L1) L2)
; 
; caught ERROR:
;   illegal function call

;     (UNION2 ((CDR L1) L2))
; 
; caught WARNING:
;   The function was called with one argument, but wants exactly two.

;     ((CDR L1) L2)
; 
; caught ERROR:
;   illegal function call

;     (UNION2 ((CDR L1) L2))
; 
; caught WARNING:
;   The function was called with one argument, but wants exactly two.
; 
; compilation unit finished
;   caught 3 ERROR conditions
;   caught 3 WARNING conditions

这篇关于LISP“错误非法函数对象...”;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆