Common Lisp: Beginner's trouble with funcall

common-lisp, lisp

Solution

Is `strategy` a variable with a functional value? If not, then use the `#'` syntax macro before it, i.e. `#'strategy`, or just (if the function is global) `'strategy`.

WHY? Because arguments of a `funcall` call are evaluated. And your `strategy` symbol is just a variable name in this case. Variable this value `'RANDOMSTRATEGY`. But you should give to `funcall` a function. How to access function if we have a symbol?

Three cases:

- Symbol may denote a variable with functional value.

- Symbol may denote a global function (`symbol-function` is the accessor in this case.

- Symbol may denote a local function (flet, labels and so on).

It looks like you forgot to define `RANDOMSTRATEGY` function.

`(defun RANDOMSTRATEGY …)`

Hmm

FUNCALL: 'RANDOMSTRATEGY

Maybe you have `(setq strategy ''RANDOMSTRATEGY)`?

Then `strategy` will evaluate to `'RANDOMSTRATEGY`. Did you notice `'` before the symbol name? `'RANDOMSTRATEGY` <=> `(quote RANDOMSTRATEGY)`; it is not a proper function name.

Problem

I'm trying to pass a function as an argument and call that function within another function. A piece of my code looks like this: ``` (defun getmove(strategy player board printflag) (setq move (funcall strategy player board)) (if printflag (printboard board)) ``` strategy is passed as a symbol represented in a two dimensional list as something such as 'randomstrategy I keep getting the error: "FUNCALL: 'RANDOMSTRATEGY is not a function name; try using a symbol instead... When I replace strategy with 'randomstrategy it works fine. I can also call randomstrategy independently. What is the problem?

Original source