They say that Lisp is a "functional" language, so you'd think it should be straightforward, but after googling around I've found comments from people to the effect that "functional programming in elisp is difficult to impossible"..

psholtz wrote:They say that Lisp is a "functional" language, so you'd think it should be straightforward, but after googling around I've found comments from people to the effect that "functional programming in elisp is difficult to impossible"..

I think lexical-let was added only recently, but yes, that does seem to be a closure.

It may be that functional programming is "next to impossible" in Elisp just because of how heavy in side-effects Emacs programming is... I don't quite buy that, but the argument might hold water.

Anyway, I hope you found this informative. I certainly did. Not all Lisps are created equal, and there are certainly arguments to be made for an Emacs implemented in CL or Scheme. Something with support for threads.

A man can dream...

Last edited by Duke on Tue Jun 14, 2011 11:41 am, edited 1 time in total.

"If you want to improve, be content to be thought foolish and stupid." -Epictetus

Lisp is not a language. Lisp is a family of very different languages using different paradigms. Emacs Lisp is not particularly functional. It does support some functional approaches, although since it uses dynamic bindings (there is a lexical-let in cl extension, but it is a hack) lexical closures are obviously impossible. Emacs Lisp is a fairly old dialect, predating Scheme, and mostly used an Emacs editor extension language.

psholtz wrote:Is there a way to pass functions as arguments in Emacs Lisp?

Emacs Lisp is a Lisp-2, which means it separates value space for values and functions. You can read about motivation for such separation in this paper. Symbols access appropriate namespace depending on their position. In operator position the function values is accessed and called, while in any other position value value is accessed.

In order to access a function without calling it you should use the special form FUNCTION. In order to call a function in value space, for example one passed as a function argument, bound to let, or created anonymously, you can use functions FUNCALL and APPLY. Refer about exact difference between them in Emacs built in help or Emacs Lisp manual. Your example can be written as:

Duke wrote:Anyway, I hope you found this informative. I certainly did. Not all Lisps are created equal, and there are certainly arguments to be made for an Emacs implemented in CL or Scheme. Something with support for threads.

I think there's an editor called "Hemlock" which purports to be an Emacs implemented in Common Lisp... although I haven't really played around w/ it.