LISP中的#是什么意思 [英] What does # mean in LISP

查看:129
本文介绍了LISP中的#是什么意思的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如#'functionname,有必要吗?

解决方案

#'Common Lisp中的函数名

通用Lisp和其他一些Lisp方言具有多个名称空间.这里的功能和值是不同的.要获取名称的功能值,我们需要编写:

(function functionname)

由于要写的时间有点长,所以有一个较短的符号:

#'functionname

要显示效果,请参见:

(let ((foo 42))
  (flet ((foo () 'bar))
    (list foo (function foo) #'foo (foo))))

上面定义了局部变量 FOO局部函数 FOO. list语句返回FOO value ,然后使用(function ...)表示法返回foo功能值,然后使用简写表示法返回相同的值,然后实际调用函数FOO的值.

(function foo)#'foo是相同的概念,但是写法不同.两者都引用名为FOO的局部词法函数. FOOFLET形式作为 local function 引入.

Lisp REPL返回如下内容:

(42 #<function FOO 4060008224> #<function FOO 4060008224> BAR)

上面显示出,第一项实际上是变量FOO value ,而后两项是 function values ,该函数绑定到FOO .最后一项是符号BAR,它是从函数调用(FOO)返回的.

Common Lisp,Emacs Lisp和ISLisp中存在正常值和函数值的命名空间分隔. Scheme等其他Lisp方言没有这种分隔.在Scheme中,名称只能表示一个值.

#字符.

#字符用于在s表达式中引入特殊语法.以下是一些示例:

#'functionname   ->  (function functionname)
#(1 2 3)         ->  the vector of the elements 1 2 3
#c(1 2)          ->  a complex number
#xFFFF           ->  a hex number
#b1111           ->  a binary number

等等. #是所谓的调度宏字符.

ANSI Common Lisp HyperSpec在第2.4.8节"Sharpsign"中描述了#字符

Common Lisp可能对向量使用了不同的语法.说[1 2 3].对于复数,它可能还使用了不同的语法. {1 2}之类的东西.但这并不能做到这一点.为什么?原因是因为Common Lisp试图节约使用该语言中的字符的成本,并且将诸如[]{}之类的字符留给用户用于自己的语法扩展. Lisp用户通常会开发嵌入式语言,并且为了使其变得更简单,Common Lisp标准试图将字符使用率降至最低,并提供了宏字符和调度宏字符的机制.

为减少字符使用量,使用单个分配字符#,然后下一个字符确定可以表示的内容. #b表示二进制数. #x表示十六进制数字. #c表示复数. #(用于向量. #'表示函数名称.再加上更多.

由于Common Lisp是一种可编程的编程语言,因此用户可以更改此字符级语法.请参见功能 SET-DISPATCH-MACRO-CHARACTER . /p>

For example, #'functionname, is it necessary?

解决方案

#'functionname in Common Lisp

Common Lisp and some other Lisp dialects have more than one namespace. Here the ones for functions and values are different. To get the function value of a name, we need to write:

(function functionname)

Since that is a bit long to write, there is a shorter notation:

#'functionname

To show the effect see this:

(let ((foo 42))
  (flet ((foo () 'bar))
    (list foo (function foo) #'foo (foo))))

Above defines a local variable FOO and a local function FOO. The list statement returns the value of FOO, then the function value of foo using the (function ...) notation, then the same using the short hand notation and then the value of actually calling the function FOO.

(function foo) and #'foo are the same concept, but written differently. Both refer to the local lexical function named FOO. FOO is introduced by the FLET form as a local function.

The Lisp REPL returns something like this:

(42 #<function FOO 4060008224> #<function FOO 4060008224> BAR)

Above shows that the first item is really the value of the variable FOO and the next two items are the function values, the function bound to FOO. The last item is the symbol BAR, as returned from the function call (FOO).

This separation of namespaces for normal values and function values is present in Common Lisp, Emacs Lisp and ISLisp. Other Lisp dialects like Scheme don't have that separation. In Scheme a name can only denote one value.

The # character.

The # character is used to introduce special syntax in s-expressions. Here are some examples:

#'functionname   ->  (function functionname)
#(1 2 3)         ->  the vector of the elements 1 2 3
#c(1 2)          ->  a complex number
#xFFFF           ->  a hex number
#b1111           ->  a binary number

and many more. # is a so-called dispatching macro character.

The ANSI Common Lisp HyperSpec describes the # character in Section 2.4.8 Sharpsign.

Common Lisp could have used a different syntax for vectors. Say [1 2 3]. It also could have used a different syntax for complex numbers. Something like {1 2}. But it does not do that. Why? The reason is because Common Lisp tries to be economical with character usage in the language and leaves characters like [, ], { and } to the user for his/her own syntax extensions. Often Lisp users develop embedded languages and to make that a bit easier, the Common Lisp standard tries to keep character usage down to a minimum and also provides the mechanism of macro characters and dispatch macro characters.

To keep character usage down, a single dispatch character # is used and the next character then determines what can be denoted. #b for binary numbers. #x for hex numbers. #c for complex numbers. #( for vectors. #' for function names. Plus many more.

Since Common Lisp is a programmable programming language, this character level syntax can be changed by the user. See the function SET-DISPATCH-MACRO-CHARACTER.

这篇关于LISP中的#是什么意思的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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