如何在函数中定义内部函数?
(defun div618 (p1 p2)
"golden split"
(let ((ratio '(0. 0.191 0.236 0.382 0.5 0.618 0.809 1.))
(price (lambda (r) (if (<= p1 p2) (+ p1 (* (- p2 p1) r)) (- p1 (* (- p1 p2) r))))))
(if (<= p1 p2)
(dolist (r (reverse ratio)) (format t "-------~d ~d-------~%" r (price r)))
(dolist (r ratio) (format t "-------~d ~d-------~%" r (price r))))))
(div618 1600 6000)
*** - EVAL: undefined function PRICE
The following restarts are available:
USE-VALUE :R1 Input a value to be used instead of (FDEFINITION 'PRICE).
RETRY :R2 Retry
STORE-VALUE :R3 Input a new value for (FDEFINITION 'PRICE).
ABORT :R4 Abort main loop
为什么不能使用内部函数“price”?
真挚地!
(defun div618 (p1 p2)
"golden split"
(let ((ratio '(0. 0.191 0.236 0.382 0.5 0.618 0.809 1.))
(price (lambda (r) (if (<= p1 p2) (+ p1 (* (- p2 p1) r)) (- p1 (* (- p1 p2) r))))))
(if (<= p1 p2)
(dolist (r (reverse ratio)) (format t "-------~d ~d-------~%" r (price r)))
(dolist (r ratio) (format t "-------~d ~d-------~%" r (price r))))))
(div618 1600 6000)
*** - EVAL: undefined function PRICE
The following restarts are available:
USE-VALUE :R1 Input a value to be used instead of (FDEFINITION 'PRICE).
RETRY :R2 Retry
STORE-VALUE :R3 Input a new value for (FDEFINITION 'PRICE).
ABORT :R4 Abort main loop
Why cannot the inner function "price" be used ?
Sincerely!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Common Lisp 对于函数和变量绑定有不同的命名空间;此页面详细解释了它: http://cl-cookbook.sourceforge.net/functions.html 。我想你想尝试的是:
未经测试!
Common Lisp has different namespaces for function and variable bindings; this page explains it in some detail: http://cl-cookbook.sourceforge.net/functions.html. I think what you want to try is something like:
Untested!