【发布时间】:2020-05-17 19:17:28
【问题描述】:
我遇到了一个问题,我有一个宏,等熵扩展,然后调用solve-format,用'x?替换列表中的NIL,然后返回一个可以传递给求解的有效表达式。
(isentropic-expansion 295 nil 1 4.2 1.4)
返回
(-
(expt
(/ 4.2 1)
(/
(- 1.4 1)
1.4))
(/ x\? 295))
这是然后放入求解函数的有效表达式
但如果我执行以下操作(假设我已将上述命令的输出设置为变量 expr
(solve expr 2)
宏不会用expr的值替换表达式符号,只是expr字符串,我得到
Wrong number of arguments: (lambda nil expr), 1
我想这是因为我还没有完全理解宏!谁能解释一下这是为什么?
(defun newton-f (func x0)
"Solve the equation FUNC(x)=0 using Newton's method.
X0 is an initial guess."
(let* ((tolerance 1e-6)
(x x0)
(dx 1e-6)
fx fpx)
(while (> (abs (funcall func x)) tolerance)
(setq fx (funcall func x)
fpx (/ (- (funcall func (+ x dx)) (funcall func (- x dx))) (* 2 dx))
x (- x (/ fx fpx))))
x))
(defmacro solve (expression guess)
`(newton-f
(lambda ,(cl-loop for item in (flatten expression)
if (and (symbolp item) (s-ends-with? "?" (symbol-name item)))
collect item)
,expression)
,guess))
(defun solve-format (exp)
(cond ((null exp) 'x?)
((atom exp) exp)
((list exp) (cons (solve-format (car exp))
(if (cdr exp)
(solve-format (cdr exp))
nil)))
(t (print "what"))))
(defmacro isentropic-expansion (t01 t02 p01 p02 gamma)
`(solve-format '(- (expt (/ ,p02 ,p01) (/ (- ,gamma 1) ,gamma)) (/ ,t02 ,t01))))
【问题讨论】: