Search code examples
common-lispclisplisp-macros

How do I fix the "X is not a number" in my macro in Lisp


I started learning lisps and am currently working on Macros for school. I created a simple macro called "-=" in a txt file called decrement.txt

(defmacro -= (numericValue decrementValue)
   (list 'setf numericValue (- numericValue decrementValue))
)

So the parameters passed are numericValue (Value that will be decremented) and decrementValue (Amount by which numericValue will be decremented by)

When I run the code in CLISP (Which is GNU CLISP 2.49), I run it like the following...

[1]> (load "decrement.txt" :echo T :print T)

;; Loading file pECLisp.txt ...
(defmacro -= (numericValue decrementValue)
    (list `setf numericValue (- numericValue decrementValue))
)
-=


;;
;; Loaded file pECLisp.txt
T
[2]> (setf x 5 y 10)
10
[3]> (-= x 1)
*** - -: X is not a number
The following restarts are available:
USE-VALUE      :R1      Input a value to be used instead.
ABORT          :R2      Abort debug loop
ABORT          :R3      Abort debug loop
ABORT          :R4      Abort main loop

What does it mean by "X is not a number", does that have to do with how the macro doesn't know the actual values of the variables yet? Because when I type in USE-VALUE and I enter 5 (Which is the value X should be) it runs perfectly fine, even when I (print x) it shows x as 4 because it was decremented in the function. So the value changes as it should, but when I initially run that "-=" function it gives that error, how do I fix that?


Solution

  • Good effort, you are only missing in the macro the evaluation of the parameters. If you use the ` (slanted quote), you can write it as:

    (defmacro -= (numericValue decrementValue)
       `(setf ,numericValue (- ,numericValue ,decrementValue))
    )
    

    Now you can do:

    (-= x 1)  => 4
    x  => 4