如何在函数中定义内部函数?

发布于 2024-12-04 18:53:00 字数 844 浏览 1 评论 0原文

(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

温柔嚣张 2024-12-11 18:53:01

Common Lisp 对于函数和变量绑定有不同的命名空间;此页面详细解释了它: http://cl-cookbook.sourceforge.net/functions.html 。我想你想尝试的是:

(defun div618 (p1 p2)
"golden split"
    (let ((ratio '(0. 0.191 0.236 0.382 0.5 0.618 0.809 1.))
          (price (function (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 (funcall price r)))
             (dolist (r ratio) (format t "-------~d  ~d-------~%" r (funcall price r))))))

未经测试!

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:

(defun div618 (p1 p2)
"golden split"
    (let ((ratio '(0. 0.191 0.236 0.382 0.5 0.618 0.809 1.))
          (price (function (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 (funcall price r)))
             (dolist (r ratio) (format t "-------~d  ~d-------~%" r (funcall price r))))))

Untested!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文