常见的Lisp错误:“应该是Lambda表达式" [英] Common lisp error: "should be lambda expression"

查看:116
本文介绍了常见的Lisp错误:“应该是Lambda表达式"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几天前我才刚开始学习Common Lisp,我正在尝试构建一个将数字插入到树中的函数.我遇到了错误,

***-SYSTEM ::%EXPAND-FORM:(CONS NIL LST)应该是lambda表达式

通过谷歌搜索,似乎在括号过多的情况下会发生这种情况,但是在看了一个小时左右并更改了周围的内容之后,我不知道该在哪里进行此操作. /p>

这是发生的代码:

(defun insert (lst probe)
    (cond ((null lst) (cons probe lst))
        ((equal (length lst) 1)
            (if (<= probe (first lst))
                (cons probe lst)
                (append lst (list probe))))
        ((equal (length lst) 2)
            ((cons nil lst) (append lst nil) (insertat nil lst 3)
                (cond ((<= probe (second lst)) (insert (first lst) probe))
                     ((> probe (fourth lst)) (insert (fifth lst) probe))
                     (t (insert (third lst) probe)))))))

我很确定它是在[[equal(length lst)2)之后发生的,其思想是在现有列表中插入一个空列表,然后将一个空列表附加到末尾,然后再插入一个空列表进入中间.

解决方案

正确!问题出在那之后,它说

((cons nil lst) (append lst nil) (insertat nil lst 3) ...

问题是两个开括号.括号可以在特殊情况下(例如您正在使用的cond形式)改变含义,但是在这种情况下,括号表示常规函数应用程序,就像您可能习惯的那样.这意味着括号后的第一件事必须是一个函数.从外部括号的角度来看,第一件事是(cons nil lst),因此它必须是一个函数(不是).

请注意,您不能只删除括号,因为cons函数会根据需要返回新列表,但不会更改旧列表.您可能想要这样的东西:

(setq lst (cons nil lst))
(setq lst (append lst nil))
(setq lst (insertat nil lst 3))
...

I just started learning Common Lisp a few days ago, and I'm trying to build a function that inserts a number into a tree. I'm getting an error,

*** - SYSTEM::%EXPAND-FORM: (CONS NIL LST) should be a lambda expression

From googling around, it seems like this happens when you have too many sets of parenthesis, but after looking at this for an hour or so and changing things around, I can't figure out where I could be doing this.

This is the code where it's happening:

(defun insert (lst probe)
    (cond ((null lst) (cons probe lst))
        ((equal (length lst) 1)
            (if (<= probe (first lst))
                (cons probe lst)
                (append lst (list probe))))
        ((equal (length lst) 2)
            ((cons nil lst) (append lst nil) (insertat nil lst 3)
                (cond ((<= probe (second lst)) (insert (first lst) probe))
                     ((> probe (fourth lst)) (insert (fifth lst) probe))
                     (t (insert (third lst) probe)))))))

I'm pretty sure it's occurring after the ((equal (length lst) 2), where the idea is to insert an empty list into the existing list, then append an empty list onto the end, then insert an empty list into the middle.

解决方案

Correct! The problem is in the line right after that, where it says

((cons nil lst) (append lst nil) (insertat nil lst 3) ...

The issue is the two opening parentheses. Parentheses can change meaning in special contexts (like the cond form you're using), but in this context, the parentheses signify regular function application, just like you're probably used to. That means the first thing after the parentheses must be a function. From the perspective of the outer parentheses, the first thing is (cons nil lst), so that must be a function (which it's not).

Note that you can't just remove the parentheses, because the cons function returns a new list like you want but doesn't change the old list. You probably want something like this:

(setq lst (cons nil lst))
(setq lst (append lst nil))
(setq lst (insertat nil lst 3))
...

这篇关于常见的Lisp错误:“应该是Lambda表达式"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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