Common lisp error: “should be lambda expression”

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))
...

Leave a Comment