let and flet in emacs lisp

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

    You don't have to use flet if you do not want to. You place a function in the function cell of a local symbol defined using let as in the following example:

    (let ((ALocalSymbol))
      (fset 'ALocalSymbol (lambda (x) (* 2 x)))
      (ALocalSymbol 4)
      )
    

    Evaluating this will return 8. Do notice the quote in front of ALocalSymbol in (let ((ALocalSymbol))...). While setq quotes symbols, fset does not.

    flet is a syntactic sugar of sorts. Using a plain-old let to define nil-valued symbols, allows you to choose which "cell" of a symbol to set. You could use setq to set the symbol's value cell or fset to set the function cell.

    Hope this helps,

    Pablo

提交回复
热议问题