# 如何改一段Scheme的closure代码到elisp？

``````#+BEGIN_SRC scheme
(define (average-damp f)
(lambda (x) (average x (f x))))
(define (average x y)
(/ (+ x y) 2))
(define (square x)
(* x x))
((average-damp square) 10)
#+END_SRC

#+RESULTS:
: 55
``````

define --> defun
/ 2 --> / 2.0

``````#+begin_src emacs-lisp :session sicp
(defun average-damp(f)
(lambda (x) (average x (f x))))
(defun average(x y)
(/ (+ x y) 2.0))
(defun square(x)
(* x x))
((average-damp 'square) 10)
#+end_src
``````

``````
(defun average-damp (f)
(lambda (x) (average x (funcall f x))))

(defun average (x y)
;; Keep the float
(/ (+ x y) 2.0))

(defun square (x)
(* x x))

(funcall (average-damp #'square) 10)
``````

``````
When you write a list as an expression in your program, you specify
which function to call, and how many arguments to give it, in the text
of the program.  Usually that’s just what you want.  Occasionally you
need to compute at run time which function to call.  To do that, use the
function ‘funcall’.  When you also need to determine at run time how
many arguments to pass, use ‘apply’.
``````

Funcall怎么用我觉得这里应该说清楚了，当你需要运行时构造一个函数，并调用的时候，就用funcall。

1 个赞

emacs lisp 的 closure 不是这么写的么 (滑稭 (每日迷惑行为 1/1

``````(closure ((x . 1) t) (x) 1)
``````
``````(defun average-damp (f*)
`(closure (,(cons 'f f*) t) (x) (average x (funcall f x))))
``````