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

时间:2011-09-15 01:07:51

标签: clisp

(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

为什么不能使用内部功能“价格”?

此致!

1 个答案:

答案 0 :(得分:1)

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))))))

未测试!