0

I want to use a function that is taking advantage of lexical-binding:

(defun comp (funcs)
  "Function composition"
  (let (f0 ff)
    (if (not funcs)
        (lambda (x &rest args)
          x)
      (progn
        (setq f0 (car funcs))
        (setq ff (cdr funcs))
        (lambda (&rest args)
          (funcall f0 (apply (comp ff) args)))))))

So setting (setq lexical-binding t) inside my buffer and calling comp with (funcall (comp (list (lambda (x) (* x 3)) (lambda (x) (+ 1 x)))) 0) gives 3 as desired.

Now I want to move comp to another lisp-file that gets loaded (via load-file) from my .emacs at the Emacs start-up. But then I get (void-variable f0) which is not what I want. I included (setq lexical-binding t) as a first line into .emacs as well as the lisp-file containing comp but this isn't solving my issue.

How can I resolve this without having to define my function comp within the same buffer were I want to call it?

NickD
  • 27,023
  • 3
  • 23
  • 42
dmw64
  • 155
  • 4
  • See also `C-h i g (elisp)Converting to Lexical Binding`. – phils May 05 '23 at 12:35
  • 2
    In short, a function which needs lexical binding must be defined in a file for which lexical binding is enabled. (You can then *call* the function from anywhere at all.) – phils May 05 '23 at 12:40
  • @phils Thanks a lot for your hints!!! Yes, you are totally right - lexical-binding is required at the definition of the function. I afraid my main issues was using `(setq lexical-binding t)` instead `;;; -*- lexical-binding: t; -*-`. The former is not working for me... – dmw64 May 05 '23 at 14:46

0 Answers0