我可以使用带有' vec&#x27的引号的表单返回未评估的列表作为向量。在Clojure中,如何使用函数参考?
这是4clojure替补中反复试验的产物,这可能是特殊的,尤其是
预期的行为:
(vec (repeat 10 0))
=> [0 0 0 0 0 0 0 0 0 0]
使用引用的形式,我得到了这个结果:
(vec '(repeat 10 0))
=> [repeat 10 0]
这令人困惑。
我可以通过将其转换回列表并使用“评估”来评估该序列,但是在返回值中,“重复”到底是什么?我可以使用(首先[重复10 0])
来访问序列的其他成员,并调用它?在4clojure repl,((第一(vec'(重复10 0))))10 0)
返回0
。
This was the product of trial and error in the 4clojure REPL, and this may be a peculiarity of that in particular,
Intended behaviour:
(vec (repeat 10 0))
=> [0 0 0 0 0 0 0 0 0 0]
Using the quoted form I get this result:
(vec '(repeat 10 0))
=> [repeat 10 0]
This is puzzling.
I can evaluate this sequence by converting it back to a list and using 'eval', but what exactly is 'repeat' here in the return value? Can I access it apart from the other members of the sequence, using (first [repeat 10 0])
for example, and call it? In the 4Clojure REPL, ((first (vec '(repeat 10 0))) 10 0)
returns 0
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
表达式
'(重复10 0)
返回引用列表(报价
停止评估)。
重复
不会是函数,而是符号:您可以使用
第一个
并仅获取此符号,但是如果要调用它,则必须将其转换为功能resolve
:如果您将使用
vector
在创建向量之前,将评估每个元素:在这种情况下,您甚至不需要
resolve
:为什么
((first(vec'(vec'(重复10 0)))) )10 0)
返回0
?因为此表达式('重复10 0)
不调用函数重复
。此表达式在某物上调用符号。您可以做到这一点,符号实现
ifn
,因此它们为“可callable” 。 可以使用 ifn? :您 功能不仅是“可呼叫” ,还包括符号,关键字,向量,地图或集合。
当符号被调用时,函数实际上称为
get
。因此,的含义('重复10 0)
是:搜索'对象中的重复
10
,如果您找不到它,返回0
。在更多示例中看到这一点:Expression
'(repeat 10 0)
returns quoted list (quote
stops evaluation).repeat
won't be function, but symbol:You can use
first
and get only this symbol, but if you want to call it, you have to convert it into function withresolve
:If you will use
vector
, each element will be evaluated before vector is created:And in this case, you don't even need
resolve
:And why does
((first (vec '(repeat 10 0))) 10 0)
returns0
? Because this expression('repeat 10 0)
doesn't call functionrepeat
.This expression calls symbol on something. You can do that, symbols implement
Ifn
, so they are "callable". You can check that withifn?
:As you can see, not only functions are "callable", but also symbols, keywords, vectors, maps or sets.
When symbol is called on something, function actually called is
get
. So, meaning of('repeat 10 0)
is this: search for'repeat
in object10
and if you don't find it, return0
. See this in more examples:引用列表
(重复10 0)
符号重复
将返回而不是clojure.core.core.core $ repot
。当您调用符号作为函数时,符号的实现说明发生了什么在您的情况下,您正在调用符号
重复
并传递两个参数10
10 和0
。第二个参数0
是notfound
在上方链接的实现中看到的值。如果您在符号重复上调用
resolve
,则可以使用返回值(##'clojure.core.core/repote
)来调用。When you quoted the list
(repeat 10 0)
the symbolrepeat
is returned instead ofclojure.core$repeat
.When you invoke a symbol as a function, this implementation of symbol explains what happens In your case you are invoking the symbol
repeat
and passing it two arguments10
and0
. The second argument0
is thenotFound
value seen in the implementation linked just above.If you call
resolve
on the symbolrepeat
, you could use the return value (#'clojure.core/repeat
) to invoke.如果您引用某些内容(使用
'
),则引用其中的所有内容。这是从解决功能等选择中选择。
因此,您可以从
'(重复10 0)
一个列表中,其中包含符号重复>重复
和编号10和0。
要在此处清楚:
重复
您看到不是 函数。这只是一个象征。如果您需要该功能,则必须
Resolve
IT。如果您想仅掌握该功能,您将不引用列表。而且总是有
eval
的选项。If you quote something (with
'
), you quote everything inside it. Thisis opting out of resolving functions etc.
So you get from
'(repeat 10 0)
a list, that contains a symbolrepeat
and the number 10 and 0.
To be clear here: the
repeat
you see there is not the function.It's just a symbol. If you want the function from it, you would have to
resolve
it. If you want to get hold just of the function, you wouldnot quote the list. And there is always the option to
eval
.