在 Clojure 和其他 Lisp 方言中,在函数名称末尾使用星号的约定是什么?
请注意,我不是在谈论符号名称中的耳罩,该问题在 Clojure 常量的约定、样式和用法? 和 clojure 中如何使用“*var-name*”命名约定?。我严格谈论的是有一些名为 foo 的函数然后调用函数 foo* 的实例。
Note that I'm not talking about ear muffs in symbol names, an issue that is discussed at Conventions, Style, and Usage for Clojure Constants? and How is the `*var-name*` naming-convention used in clojure?. I'm talking strictly about instances where there is some function named foo that then calls a function foo*.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 Clojure 中,它基本上意味着“foo* 类似于 foo,但有些不同,并且您可能想要 foo”。换句话说,这意味着该代码的作者无法为第二个函数想出更好的名称,因此他们只是在其上打了一颗星。
In Clojure it basically means "foo* is like foo, but somehow different, and you probably want foo". In other words, it means that the author of that code couldn't come up with a better name for the second function, so they just slapped a star on it.
数学家和 Haskeller 可以使用撇号来表示相似的对象(值或函数)。相似但不完全相同。彼此相关的对象。例如,函数
foo
可以以一种方式进行计算,而foo'
会使用不同的方法得出相同的结果。也许这个命名缺乏想象力,但它有数学根源。Lisp 通常(没有任何最终原因)会丢弃符号名称中的撇号,并且
*
有点类似于撇号。 Clojure 1.3 最终将通过允许在名称中使用撇号来解决这个问题!Mathematicians and Haskellers can use their apostrophes to indicate similar objects (values or functions). Similar but not quite the same. Objects that relate to each other. For instance, function
foo
could be a calculation in one manner, andfoo'
would do the same result but with a different approach. Perhaps it is unimaginative naming but it has roots in mathematics.Lisps generally (without any terminal reason) have discarded apostrophes in symbol names, and
*
kind of resembles an apostrophe. Clojure 1.3 will finally fix that by allowing apostrophes in names!如果我正确理解你的问题,我已经看到过使用
foo*
来表明该函数在理论上与另一个函数等效的实例,但使用不同的语义。以 lamina 库为例,它定义了map*
、filter*,
take*
为其核心类型,通道。通道与 seq 非常相似,因此这些函数的名称有意义,但它们不够兼容,因此它们本身应该“相等”。我见过的 foo* 风格的另一个用例是使用额外参数调用辅助函数的函数。例如,
fact
函数可能会委托给fact*
,如果以递归方式编写,它会接受另一个参数(累加器)。您不一定希望在fact
中暴露有一个额外的参数,因为调用(fact 5 100)
不会为您计算 5- 的阶乘-暴露额外的参数是一个错误。我也看到过相同风格的宏。宏
foo
扩展为对foo*
的函数调用。If I understand your question correctly, I've seen instances where
foo*
was used to show that the function is equivalent to another in theory, but uses different semantics. Take for instance the lamina library, which defines things likemap*
,filter*
,take*
for its core type, channels. Channels are similar enough to seqs that the names of these functions make sense, but they are not compatible enough that they should be "equal" per se.Another use case I've seen for
foo*
style is for functions which call out to a helper function with an extra parameter. Thefact
function, for instance, might delegate tofact*
which accepts another parameter, the accumulator, if written recursively. You don't necessarily want to expose infact
that there's an extra argument, because calling(fact 5 100)
isn't going to compute for you the factorial of 5--exposing that extra parameter is an error.I've also seen the same style for macros. The macro
foo
expands into a function call tofoo*
.普通的 let 绑定
(let ((...)))
并行创建单独的变量let star 绑定
(let* ((...)))
创建变量顺序地这样可以从彼此计算出来,这样我可能有点偏离基础,但请参阅 LET 与LET* in Common Lisp 了解更多详细信息
编辑:我不确定这在 Clojure 中如何反映,我才开始阅读《Programming Clojure》,所以我还不知道
a normal let binding
(let ((...)))
create separate variables in parallela let star binding
(let* ((...)))
creates variables sequentially so that can be computed from eachother like soI could be slightly off base but see LET versus LET* in Common Lisp for more detail
EDIT: I'm not sure about how this reflects in Clojure, I've only started reading Programming Clojure so I don't know yet