让我们在的Emacs Lisp FLET [英] let and flet in emacs lisp

查看:163
本文介绍了让我们在的Emacs Lisp FLET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道你是否会称之为规范的制定,而是结合当地的功能,我由GNU手册建议使用FLET

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))
)

然而,意外我尝试以下EX pression,在这里我使用'让'绑定的lambda前pression一个变量(方案已经打了位之后),它也适用,如果我传给mapcar *功能:

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)) ))
)

和这两个函数工作:

(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.

推荐答案

与计划时,Emacs Lisp是一个的 2口齿不清的,这意味着每个符号有两个独立的绑定:值绑定和功能结合。在函数调用(ABCD),第一个符号( A )的查找使用功能绑定,其余( BCD )的查找使用值绑定。特殊的形式创建一个新的(本地)值绑定, FLET 创建一个新的功能结合。

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.

请注意该值或功能用于查找绑定是否在(ABCD)函数调用依赖于的位置的,而不是在键入的看着的向上价值。特别是,一个值绑定可以解析功能

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.

在你的第一个例子,你的功能绑定˚F(通过 FLET ),然后做一个函数查找

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

(f ...)

在你的第二个例子,你值绑定˚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.

<一个href=\"http://en.wikipedia.org/wiki/Common_Lisp#Comparison_with_other_Lisps\">http://en.wikipedia.org/wiki/Common_Lisp#Comparison_with_other_Lisps

这篇关于让我们在的Emacs Lisp FLET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆