Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to return function in elisp

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.

like image 618
RNA Avatar asked Feb 16 '23 15:02

RNA


1 Answers

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"

like image 107
sds Avatar answered Mar 05 '23 20:03

sds