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

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))))

Tags:

Emacs

Elisp