简体   繁体   中英

elisp , passing functions as arguments and call it, an Eval error happend, why?

my elisp program is:

(defun test (f x) (f x))
(test (lambda (x) (* x x)) 10)

run it, an error happened:

* Eval error * Symbol's function definition is void: f

Emacs is a lisp-2, so has a different namespace for functions and variables. So, in test, the f in the second (fx) is not the same as the (f) in the parameter list.

Try

(defun test (f x) (funcall f x))

All is good.

Here's the correction:

(defun test (f x)
  (funcall f x))

(test (lambda (x) (* x x)) 10)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM