Alex
Alex

Reputation: 387

How can I define the LAMBDA function in LISP?

I can't use at all lambda function, because I get this error: Argument to apply/funcall is not a function: (LAMBDA (E) (COUNT_ATOMS M E)).

And the code is:

> (DEFUN count_atoms (m l)
>     (COND ((ATOM l) (COND ((EQUAL m l) 1)
>                       (t 0)))
>       (t (APPLY '+
>                 (MAPCAR '(LAMBDA (e) (count_atoms m e))
>                         l)))))

For

(count_atoms 3 '( (3 3) 3 (4 4) 5))

it should print 3.

What is wrong here?

Upvotes: 1

Views: 376

Answers (1)

Will Ness
Will Ness

Reputation: 71119

use (MAPCAR #'(LAMBDA (e) (count_atoms m e)) ..., with the "sharp" sign. Same with apply, use #'+.

writing #'(lambda .... ) is the same as writing (function (lambda .... )). There's also a macro lambda which lets you get away with writing just (lambda ... ) there.

see also:

Upvotes: 1

Related Questions