The task is as follows: depending on the passed function value, with the help of perform either the addition of the list elements or the multiplication of the list elements. those.

apply func(x) '(1 2 3 4) 

if x = 1, then the result is 10, if x = 2, then 24

Here is what I did:

I define the func function

 (defun func_(x) (cond ((eql 1 x)(plus))(eql 2 x)(TIMES)) 

then try to call apply

 (apply func_(1) '(1 2 3 4)) 

but the compiler gives an error

Error: The variable FUNC_ is unbound.
Fast links are on: do (si :: use-fast-links nil) for debugging
Error signalled by EVAL.
Backtrace: EVAL
Broken at SYSTEM :: GCL-TOP-LEVEL.

  • one
    Use the "Fragment of the code" button only for the code that can actually be executed in the browser. For non-self-sufficient pieces of code, you should use blocks of code that are formatted with an indent of 4 spaces (Ctrl + K). - Mikhail Vaysman

1 answer 1

There are no plus and TIMES functions in my interpreter (more precisely, TIMES was previously defined differently), but I immediately see a strange thing for you - you are trying to call func_ not in the style of LISP. Try replacing your

 (apply func_(1) '(1 2 3 4)) 

on

 (apply (func_ 1) '(1 2 3 4)) 

I also decided to rewrite your func_ function using + and *, it turned out the following:

 (defun func_(x) (cond ((eql 1 x)(lambda (&rest ns) (apply '+ ns)))((eql 2 x)(lambda (&rest ns) (apply '* ns)))))