“foop”:命名约定?它是“foo”的辅助递归函数;后缀“p”是什么意思意思是?
我遇到了以下代码片段(函数定义):
choose (x:xs) = choosep x xs
where choosep x [] = x
choosep x (_:_) = x
choosep _ (x:xs) = choosep x xs
在“标准库”中的Curry 编程语言--/usr/lib/curry-0.9 .11/Success.curry 来自 Muenster Curry 编译器。这里:
choose :: [a] -> a
和
choosep :: a -> [a] -> a -- BTW, not a _p_redicate
辅助递归函数 choosep
的 “p”后缀 是已知的命名约定吗?也许它来自函数式编程传统(Haskell)或逻辑编程(Prolog?)。那是什么意思呢?
(这个函数在 为什么 Curry 的 std lib 中的非确定性选择函数没有直接定义而是使用辅助 2 参数函数来定义?。)
I've come across the following code snippet (a function definition):
choose (x:xs) = choosep x xs
where choosep x [] = x
choosep x (_:_) = x
choosep _ (x:xs) = choosep x xs
in Curry programming language in a "standard library"--/usr/lib/curry-0.9.11/Success.curry from Muenster Curry Compiler. Here:
choose :: [a] -> a
and
choosep :: a -> [a] -> a -- BTW, not a _p_redicate
Is the "p" suffix for the helper recursive function choosep
a known naming convention? Perhaps it comes from functional programming tradition (Haskell) or logical programming (Prolog?). What does it mean then?
(This function was considered in Why is the non-deterministic choice function in Curry's std lib not defined straightforwardly but rather with a helper 2-argument function?.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在这种情况下,我相信
p
代表“prime”。他们没有调用帮助器choose'
或chooseprime
,而是使用choosep
。In this case, I believe
p
stands for "prime". Rather than calling the helperchoose'
orchooseprime
, they usechoosep
.我认为它代表“prime”——在 OCaml 中,它允许在标识符中使用
'
,辅助函数通常被命名为foo'
。在较高的层面上,我认为这(以及使用“where”来进行事后辅助定义)源于允许函数式程序类似于纯数学中的等效定义的愿望。I think it stands for 'prime' -- in OCaml, which allows
'
in identifiers, helper functions are frequently namedfoo'
. At a high level, I think this (and the use of 'where' for a post-hoc helper definition) stems from the desire to allow functional programs to resemble their equivalent definitions in pure math.在这种情况下,正如其他人所指出的,它可能不适用,但有一个流行的 Lisp 约定,即使用最后的“p”来表示谓词。请参阅行话 p-convention。
我个人更喜欢用 '?' 结束谓词的 Ruby 约定。
In this context, as others have noted, it probably doesn't apply, but there is a popular Lisp convention of using a final 'p' to denote a predicate. See jargon p-convention.
I personally prefer the Ruby convention of ending a predicate with a '?'.
P 代表“谓词”。返回“真”或“假”的事物。
P stands for 'predicate'. A thing that returns 'true' or 'false'.