Higher-order functions in Elisp

佐手、 提交于 2019-12-03 12:38:24

From the NEWS for Emacs 24:

Lisp changes in Emacs 24.1

  • Code can now use lexical scoping by default instead of dynamic scoping. The lexical-binding variable enables lexical scoping for local variables. It is typically set via a file-local variable in the first line of the file, in which case it applies to all the code in that file.

So, in Emacs 24:

(setq lexical-binding t)
(defun singleton-set (elem) (lambda (n) (= n elem)))
(mapcar (singleton-set 1) '(0 1 2 3))
    ===> (nil t nil nil)

How to make functions returning functions possible in Emacs Lisp?

Using fake closures, and lexical-let.

What is the reason this mechanism is different from other languages like Python, Scala or Clojure?

Richard Stallman answered this question in a paper he wrote a while ago.

Marcin
(defun singleton-set (elem)
  `(lambda (n) (= n ,elem))

See: elisp functions as parameters and as return value

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