Generic Lisp: beginner problem with funcall

I am trying to pass a function as an argument and call that function in another function.

Part of my code looks like this:

(defun getmove(strategy player board printflag)
(setq move (funcall strategy player board))
(if printflag
    (printboard board))
Strategy

passed as a character represented in a two-dimensional list, like something like "randomstrategy

I keep getting the error: "FUNCALL:" RANDOMSTRATEGY is not a function name, try using the symbol instead ...

When I replace the "randomstrategy" strategy, it works fine. I can also name a random strategy myself. What is the problem?

+3
source share
4 answers

? # ' . #. ( ) "

? funcall call evaluted. - . Variabe "RANDOMSTRATEGY. funcall . , ?

:

1) Symbol may denote variable with functional value
2) Symbol may denote global function (symbol-function - is the accessor in this case.
3) Symbol may denote local function (flet, labels and so on)

, RANDOMSTRATEGY

(defun RANDOMSTRATEGY ..)

FUNCALL: 'RANDOMSTRATEGY

, ( setq '' RANDOMSTRATEGY)?

"RANDOMSTRATEGY. " ? 'RANDOMSTRATEGY <= > (quote RANDOMSTRATEGY)

+2

, strategy randomstrategy, (!) 'randomstrategy ( (quote randomstrategy)).

, , second, , , , - . , , getmove, 'randomstrategy, randomstrategy, . (, ?)

, , (funcall 'randomstrategy ...): 'randomstrategy, , , randomstrategy.

+4

? .

(setq strategy 'randomstrategy)
(setq move (funcall strategy player board))
+1
source

Without seeing the code, I imagine that you are doing something like this:

(defun randomstrategy (abc) ...)

and then do the following:

(getmove 'randomstrategy xyz)

What you want to do is pass the "randomstrategy" function to getmove with # ':

(getmove # 'randomstrategy xyz)

In CommonLisp, # 'returns the function associated with the character that you want to pass to getmove.

0
source

Source: https://habr.com/ru/post/1721117/


All Articles