I\'ve written a Lisp function like this:
(defun power (base exponent)
(if (= exponent 0)
1
(* base (power (- exponent 1)))))
Wh
It is the recursive call that only has one argument:
(power (- exponent 1))
It should be like this:
(power base (- exponent 1))
The recursive call is your problem. You forgot to pass the base in as the first argument.
(* base (power (- exponent 1)))))
should be:
(* base (power base (- exponent 1)))))
Lisp comes with the function expt, so no need to define your own.
(Unless this is an exercise or homework, in which case you might want to look at more efficient methods, such as exponentiation by squaring.)
Compile your functions. In LispWorks use c-sh-c to compile a definition in the editor.
Here in the REPL:
CL-USER 18 > (defun power (base exponent)
(if (= exponent 0)
1
(* base (power (- exponent 1)))))
POWER
CL-USER 19 > (compile 'power)
;;;*** Warning in POWER: POWER is called with the
;;; wrong number of arguments: Got 1 wanted 2
The Compiler will already tell you that there is a problem with the code.
Note that the LispWorks Listener (the REPL) does not compile. You have to compile the definitions you enter at the Listener with the function COMPILE. Alternatively you can type the definitions into an editor window and compile it from there (by either compiling the file, the buffer or the expression). LispWorks also has features to locate code where an error happens.