公共Lisp与Scheme中函数和变量的独立命名空间 [英] Separate Namespaces for Functions and Variables in Common Lisp versus Scheme

查看:161
本文介绍了公共Lisp与Scheme中函数和变量的独立命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

方案将所有变量使用单个命名空间,无论它们是绑定到函数还是其他类型的值. Common Lisp将两者分开,因此标识符"hello"可以在一个上下文中引用一个函数,而在另一个上下文中引用一个字符串.

Scheme uses a single namespace for all variables, regardless of whether they are bound to functions or other types of values. Common Lisp separates the two, such that the identifier "hello" may refer to a function in one context, and a string in another.

(注1:这个问题需要上面的例子;可以随时对其进行编辑并添加一个,或者通过电子邮件将原始作者发送给我,我将这样做.)

(Note 1: This question needs an example of the above; feel free to edit it and add one, or e-mail the original author with it and I will do so.)

但是,在某些情况下,例如将函数作为参数传递给其他函数,程序员必须通过使用#'来明确地区分他是在指定函数变量,而不是非函数变量,例如:

However, in some contexts, such as passing functions as parameters to other functions, the programmer must explicitly distinguish that he's specifying a function variable, rather than a non-function variable, by using #', as in:

(sort (list '(9 A) '(3 B) '(4 C)) #'< :key #'first)

我一直认为这有点像疣,但最近我遇到了

I have always considered this to be a bit of a wart, but I've recently run across an argument that this is actually a feature:

...这 重要的区别实际上在于形式的语法,而不在于形式 对象的类型.不知道任何有关运行时值的信息 涉及到,很明显,函数形式的第一个元素 必须是一个函数. CL采纳了这一事实,并将其纳入 语言以及宏格式和特殊格式,这些格式也可以(并且必须) 静态确定.所以我的问题是:你为什么要 函数名称和变量名称要相同 名称空间,当函数名称的主要用途出现在 变量名很少会出现?
...the important distinction actually lies in the syntax of forms, not in the type of objects. Without knowing anything about the runtime values involved, it is quite clear that the first element of a function form must be a function. CL takes this fact and makes it a part of the language, along with macro and special forms which also can (and must) be determined statically. So my question is: why would you want the names of functions and the names of variables to be in the same namespace, when the primary use of function names is to appear where a variable name would rarely want to appear?

考虑类名的情况:为什么一个名为FOO的类应防止 使用名为FOO的变量?我唯一一次提到的是 名称为FOO的class在需要类名的上下文中.如果在 在极少数情况下,我需要获取绑定到该类的类对象 类名FOO,有FIND-CLASS.
Consider the case of class names: why should a class named FOO prevent the use of variables named FOO? The only time I would be referring the class by the name FOO is in contexts which expect a class name. If, on the rare occasion I need to get the class object which is bound to the class name FOO, there is FIND-CLASS.

根据经验,这种说法对我来说确实有意义;在Haskell中也有类似的情况,其中包含字段名称,这也是用于访问字段的函数.这有点尴尬:

This argument does make some sense to me from experience; there is a similar case in Haskell with field names, which are also functions used to access the fields. This is a bit awkward:

data Point = Point { x, y :: Double {- lots of other fields as well --} }
isOrigin p = (x p == 0) && (y p == 0)

这可以通过一些额外的语法来解决,通过NamedFieldPuns扩展使其特别好:

This is solved by a bit of extra syntax, made especially nice by the NamedFieldPuns extension:

isOrigin2 Point{x,y} = (x == 0) && (y == 0)

那么,除了一致性之外,对于Common Lisp vs. Scheme而言,对于所有值而言,使用单一名称空间与对函数和非函数值使用单独名称空间相比,在优点和缺点方面,有什么优点和缺点?

So, to the question, beyond consistency, what are the advantages and disadvantages, both for Common Lisp vs. Scheme and in general, of a single namespace for all values versus separate ones for functions and non-function values?

推荐答案

两种不同的方法具有名称:Lisp-1和Lisp-2. Lisp-1具有用于变量和函数的单个命名空间(如Scheme),而Lisp-2具有用于变量和函数的单独命名空间(如Common Lisp).我之所以这样说是因为您可能没有意识到该术语,因为您在问题中未曾提及该术语.

The two different approaches have names: Lisp-1 and Lisp-2. A Lisp-1 has a single namespace for both variables and functions (as in Scheme) while a Lisp-2 has separate namespaces for variables and functions (as in Common Lisp). I mention this because you may not be aware of the terminology since you didn't refer to it in your question.

Wikipedia 指的是这场辩论:

Wikipedia refers to this debate:

在Lisp社区中,是否有一个单独的函数名称空间是否是一个优势是争执的源泉.通常将其称为Lisp-1与Lisp-2辩论. Lisp-1是指Scheme的模型,而Lisp-2是指Common Lisp的模型.这些名称是由Richard P. Gabriel和Kent Pitman在1988年的论文中创造的,该论文广泛地比较了这两种方法.

Whether a separate namespace for functions is an advantage is a source of contention in the Lisp community. It is usually referred to as the Lisp-1 vs. Lisp-2 debate. Lisp-1 refers to Scheme's model and Lisp-2 refers to Common Lisp's model. These names were coined in a 1988 paper by Richard P. Gabriel and Kent Pitman, which extensively compares the two approaches.

Gabriel和Pitman的论文,标题为功能单元和价值单元中的技术分离问题解决了这个问题.

Gabriel and Pitman's paper titled Technical Issues of Separation in Function Cells and Value Cells addresses this very issue.

这篇关于公共Lisp与Scheme中函数和变量的独立命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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