如何修复 Lisp 宏中的 "X is not a number"
How do I fix the "X is not a number" in my macro in Lisp
我开始学习口齿不清,目前正在为学校开发宏。我在名为 decrement.txt
的 txt 文件中创建了一个名为“-=”的简单宏
(defmacro -= (numericValue decrementValue)
(list 'setf numericValue (- numericValue decrementValue))
)
所以传递的参数是numericValue(将递减的值)和decrementValue(numericValue将递减的量)
当我 运行 CLISP 中的代码(即 GNU CLISP 2.49)时,我 运行 它像下面这样...
[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
"X is not a number" 是什么意思,这与宏如何还不知道变量的实际值有关吗?因为当我输入 USE-VALUE
并输入 5(X 应该是这个值)时,它 运行 非常好,即使我 (print x)
它显示 x 为 4,因为它被递减了在函数中。所以值会按应有的方式更改,但是当我最初 运行 那个“-=”函数时它给出了那个错误,我该如何解决这个问题?
辛苦了,你只是在宏中缺少对参数的评估。如果你使用`(斜引号),你可以写成:
(defmacro -= (numericValue decrementValue)
`(setf ,numericValue (- ,numericValue ,decrementValue))
)
现在您可以:
(-= x 1) => 4
x => 4
我开始学习口齿不清,目前正在为学校开发宏。我在名为 decrement.txt
的 txt 文件中创建了一个名为“-=”的简单宏(defmacro -= (numericValue decrementValue)
(list 'setf numericValue (- numericValue decrementValue))
)
所以传递的参数是numericValue(将递减的值)和decrementValue(numericValue将递减的量)
当我 运行 CLISP 中的代码(即 GNU CLISP 2.49)时,我 运行 它像下面这样...
[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
"X is not a number" 是什么意思,这与宏如何还不知道变量的实际值有关吗?因为当我输入 USE-VALUE
并输入 5(X 应该是这个值)时,它 运行 非常好,即使我 (print x)
它显示 x 为 4,因为它被递减了在函数中。所以值会按应有的方式更改,但是当我最初 运行 那个“-=”函数时它给出了那个错误,我该如何解决这个问题?
辛苦了,你只是在宏中缺少对参数的评估。如果你使用`(斜引号),你可以写成:
(defmacro -= (numericValue decrementValue)
`(setf ,numericValue (- ,numericValue ,decrementValue))
)
现在您可以:
(-= x 1) => 4
x => 4