了解常见Lisp中的引号 [英] Understanding sharp quote in common lisp
问题描述
在下面的实验中,我将REPL返回错误的地方缩写为&.添加了[num],因此可以在讨论中参考.
In my experiments below I've abbreviated where the REPL returns an error, & added [num] so these can be referenced in discussion.
我对为什么调用存储在变量中的函数的尝试失败感到困惑.在我看来,语法比需要的复杂.
I'm a bit confused as to why my attempts to call a function stored in a variable are failing. It seems to me that the syntax is more complex than it needs to be.
为什么我既不发布(f 3)
,也不发布(#'f 3)
?不允许将尖引号作为表格的第一个元素?为什么在这里需要 funcall
?
Why can I issue neither (f 3)
nor even (#'f 3)
?
Is sharp quote not allowed as the first element of a form?
Why is funcall
required here?
[235]> (setf f #'abs) ; I'm ok with this
#<SYSTEM-FUNCTION ABS>
[236]> (abs 3) ; This is fine
3
[237]> (f 3) ; Err due to sep. fn namespace. OK.
-- Err[1]: "Undefined function f" --
[238]> (#'f 3) ; Don't get what this err is telling me...
-- Err[2]: "#'F is not a function name, try using a symbol instead"
[239]> (funcall #'f 3) ; seems very long winded...!
3
这是否意味着系统功能与用户定义功能的区别?
Does this mean system functions are treated differently from user defined functions?
出于完整性考虑:
[240]> (funcall abs 3)
-- Err[3]: variable ABS has no value -- ; I get why this is an error.
[241]> (funcall #'abs 3) ; Isn't this verbose... ?
3
我还没有进入ANSI Common Lisp中的符号一章,也许对您有所帮助.
I haven't got to the symbols chapter in ANSI Common Lisp yet, maybe that will help... thanks for any tips.
推荐答案
[235]> (setf f #'abs) ; I'm ok with this
#<SYSTEM-FUNCTION ABS>
在上述类型中,将一个名为 f
的变量设置为一个函数对象-来自名为 abs
的函数.
Above kind of sets a variable named f
to a function object - from the function called abs
.
[236]> (abs 3) ; This is fine
3
上面调用了函数 abs
.
[237]> (f 3) ; Err due to sep. fn namespace. OK.
上面:没有名为 f
的函数.
Above: there is no function named f
.
-- Err[1]: "Undefined function f" --
[238]> (#'f 3) ; Don't get what this err is telling me...
-- Err[2]: "#'F is not a function name, try using a symbol instead"
以上:Common Lisp仅接受符号作为函数名称,符号作为宏名称,符号作为特殊运算符或lambda表达式作为cons形式的第一个元素.(function f)
不是函数名称.
Above: Common Lisp accepts only symbols as function names, symbols as macro names, symbols as special operators or lambda expressions as the first element of a cons form. (function f)
is not a function name.
这是否意味着系统功能与用户定义功能的区别?
Does this mean system functions are treated differently from user defined functions?
否.
[239]> (funcall #'f 3) ; seems very long winded...!
3
上面用命名函数 f
中的函数对象调用函数 funcall
.然后 funcall
会以 3
作为参数来调用此函数对象.
Above calls the function funcall
with the function object from the named function f
. funcall
then calls this function object with 3
as the argument.
似乎缠绕很久
是的.
为什么我既不发行(f 3)也不发行(#'f 3)?不允许将尖引号作为表格的第一个元素吗?
Why can I issue neither (f 3) nor even (#'f 3)? Is sharp quote not allowed as the first element of a form?
因为 f
没有命名函数.它命名一个变量.#'f
也不是函数名称.我们必须使用函数名(实际上是一个符号).
Because f
is not naming a function. It names a variable. #'f
is also not a function name. We are required to use a function name (a symbol actually).
命名空间
公用Lisp(与其他Lisp方言一样)有两个用于函数和变量的命名空间.
Common Lisp (like some other Lisp dialects) has two namespaces for functions and for variables.
定义变量 foo
:
CL-USER 54 > (defvar foo 3)
FOO
定义一个函数 foo
:
CL-USER 55 > (defun foo (x) (* foo 10))
FOO
我们可以使用从变量 foo
获得的值调用函数 foo
:
We can call the function foo
with the value obtained from the variable foo
:
CL-USER 56 > (foo foo)
30
如何从函数的全局名称获取函数对象:
How to get the function object from the global name of the function:
CL-USER 57 > (fdefinition 'foo)
#<interpreted function FOO 4060001CAC>
CL-USER 58 > (symbol-function 'foo)
#<interpreted function FOO 4060001CAC>
与上述相同,但用一个简短的符号表示:
Same as above, but with a short notation:
CL-USER 58a > #'foo
#<interpreted function FOO 4060001CAC>
CL-USER 59 > (function foo) ; works also for local functions
#<interpreted function FOO 4230008AAC>
如何从全局变量获取值:
How to get a value from a global variable:
CL-USER 60 > (symbol-value 'foo)
3
或者只使用变量:
CL-USER 61 > foo
3
一些肯定:
正面:没有名称冲突.
我们可以写
(defun foo (list) (list list))
也不必写
(defun foo (lst) (list lst))
正面:更简单的编译
(let ((list 3))
(list 1 list 3))
以上内容在Common Lisp中永远不会是一个错误.在Scheme中,这将是一个错误: 3不是函数
.
Above will never be an error in Common Lisp. In Scheme it would be an error: 3 is not a function
.
这篇关于了解常见Lisp中的引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!