how to return function in elisp

谁说我不能喝 提交于 2019-12-11 01:41:52

问题


This is related to this question: elisp functions as parameters and as return value

(defun avg-damp (n)
   '(lambda(x) (/ n 2.0)))

Either

(funcall (avg-damp 6) 10)

or

((avg-damp 6) 10)

They gave errors of Symbol's value as variable is void: n and eval: Invalid function: (avg-damp 6) respectively.


回答1:


The reason the first form does not work is that n is bound dynamically, not lexically:

(defun avg-damp (n)
  (lexical-let ((n n))
    (lambda(x) (/ x n))))
(funcall (avg-damp 3) 12)
==> 4

The reason the second form does not work is that Emacs Lisp is, like Common Lisp, a "lisp-2", not a "lisp-1"



来源:https://stackoverflow.com/questions/16344077/how-to-return-function-in-elisp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!