Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

let and flet in emacs lisp

Tags:

binding

elisp

I don't know if you would call it the canonical formulation, but to bind a local function I am advised by the GNU manual to use 'flet':

(defun adder-with-flet (x)   (flet ( (f (x) (+ x 3)) )     (f x)) ) 

However, by accident I tried (after having played in Scheme for a bit) the following expression, where I bind a lambda expression to a variable using 'let', and it also works if I pass the function to mapcar*:

(defun adder-with-let (x)   (let ( (f (lambda (x) (+ x 3))) )     (car (mapcar* f (list x)) )) ) 

And both functions work:

(adder-with-flet 3)   ==> 6 (adder-with-let 3) ==> 6 

Why does the second one work? I cannot find any documentation where 'let' can be used to bind functions to symbols.

like image 659
hatmatrix Avatar asked Jul 28 '09 22:07

hatmatrix


People also ask

How do I Lisp in emacs?

In a fresh Emacs window, type ESC-x lisp-interaction-mode . That will turn your buffer into a LISP terminal; pressing Ctrl+j will feed the s-expression that your cursor (called "point" in Emacs manuals' jargon) stands right behind to LISP, and will print the result.

What does #' mean in Emacs Lisp?

#'... is short-hand for (function ...) which is simply a variant of '... / (quote ...) that also hints to the byte-compiler that it can compile the quoted form as a function.

What does CL do in Emacs?

The CL package adds a number of Common Lisp functions and control structures to Emacs Lisp. While not a 100% complete implementation of Common Lisp, it adds enough functionality to make Emacs Lisp programming significantly more convenient.

Is Emacs Lisp the same as Lisp?

By default, Common Lisp is lexically scoped, that is, every variable is lexically scoped except for special variables. By default, Emacs Lisp files are dynamically scoped, that is, every variable is dynamically scoped.


1 Answers

Unlike Scheme, Emacs Lisp is a 2-lisp, which means that each symbol has two separate bindings: the value binding and the function binding. In a function call (a b c d), the first symbol (a) is looked up using a function binding, the rest (b c d) are looked up using the value binding. Special form let creates a new (local) value binding, flet creates a new function binding.

Note that whether value or function binding is used for lookup depends on the position in the (a b c d) function call, not on the type of the looked-up value. In particular, a value binding can resolve to function.

In your first example, you function-bind f (via flet), and then do a function lookup:

(f ...) 

In your second example, you value-bind f to a function (via let), and then use a value lookup:

(... f ...) 

Both work because you use the same kind of binding and lookup in each case.

http://en.wikipedia.org/wiki/Common_Lisp#Comparison_with_other_Lisps

like image 67
user58804 Avatar answered Sep 26 '22 15:09

user58804



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!