Higher-order functions in Elisp

后端 未结 3 727
盖世英雄少女心
盖世英雄少女心 2021-02-14 08:56

I created a function that returns a function in Elisp:

(defun singleton-set (elem)
  (defun f (n) (= n elem))
  f)

I try to run this in IELM, a

相关标签:
3条回答
  • 2021-02-14 09:19
    (defun singleton-set (elem)
      `(lambda (n) (= n ,elem))
    

    See: elisp functions as parameters and as return value

    0 讨论(0)
  • 2021-02-14 09:23

    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.

    0 讨论(0)
  • 2021-02-14 09:32

    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)
    
    0 讨论(0)
提交回复
热议问题