Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Common Lisp: Beginner's trouble with funcall

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?

like image 670
Chad Avatar asked Feb 28 '23 03:02

Chad


1 Answers

The problem is that the variable strategy does not contain the symbol randomstrategy but rather the list (!) 'randomstrategy (which is a shorthand notation for (quote randomstrategy)).

Now, you could, of course, extract the symbol from the list by way of the function second, but that would only cover the real problem up, which is probably somewhere up the call chain. Try to determine why the argument that is passed to function getmove is 'randomstrategy, not randomstrategy as it should be. (Maybe you erroneously used a quote inside of a quoted list?)

Oh, and don't let yourself be confused by the fact that (funcall 'randomstrategy ...) works: the expression 'randomstrategy does not, after all, evaluate to itself, but to the symbol randomstrategy.

like image 171
Matthias Benkard Avatar answered Mar 07 '23 01:03

Matthias Benkard