Search code examples
emacselisp

Is there a way to write function to compose functions in Elisp?


I wanted to create a function, that returns a composition of several other functions, such that

(funcall (compose 'f 'g) x) == (f (g x))

I feel that I miserably failed on this. My best attempt so far:

(defun compose (funcs)
  "composes several funcitons into one"
  (lambda (arg)
    (if funcs
        (funcall (car funcs) (funcall (compose (cdr funcs)) arg))
        arg)))

But for some reason, the following still returns 0:

(funcall (compose '(
           (lambda (a) (* a 3))
           (lambda (a) (+ a 2))
)) 0)

Is there a way to fix that?


Solution

  • Your code requires lexically scoped lambdas, which Emacs Lisp doesn't support by default. If you use Emacs 24, set lexical-binding to t and your example will work as written.

    If you're using an older Emacs, you can create a lexically scoped lambda using an explicit lexical-let form:

    (require 'cl)
    
    (defun compose (funcs)
      "composes several funcitons into one"
      (lexical-let ((funcs funcs))
        (lambda (arg)
          (if funcs
              (funcall (car funcs) (funcall (compose (cdr funcs)) arg))
            arg))))