let and flet in emacs lisp

前端 未结 4 1049
小鲜肉
小鲜肉 2021-01-30 13:31

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         


        
4条回答
  •  滥情空心
    2021-01-30 13:44

    I did a quick search of the Emacs lisp manual and couldn't find any reference to 'flet, which isn't terribly surprising since that is a part of cl - the common-lisp package.

    let will do a local binding as well, but it won't bind to the "function cell" for that symbol.

    i.e. This works:

    (let ((myf (lambda (x) (list x x))))
      (eval (list myf 3)))
    

    but

    (let ((myf (lambda (x) (list x x))))
      (myf 3))
    

    fails with the error: "Lisp error: (void-function myf)"

    flet on the other hand, does do the binding to the function cell, so this works:

    (flet ((myf (x) (list x x)))
      (myf 3))
    

    Notice the difference being that flet allows you to use the symbol myf directly, whereas the let does not - you have to use some indirection to get the function out of the "value cell" and apply that appropriately.

    In your example, the 'mapcar' did the equivalent to my use of 'eval.

提交回复
热议问题