let and flet in emacs lisp

前端 未结 4 1037
小鲜肉
小鲜肉 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:50

    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

提交回复
热议问题