reverse :: [a] -> [a]
reverse = reverse' []
where reverse' :: [a] -> [a] -> [a]
reverse' result [] = result
reverse' result (x:xs) = reverse' (x:result) xs
You can call the helper function anything you want, and it won't matter as long as you don't put the helper function in the "global" namespace. Simply adding a "prime" seems a common practice. :) E.g., in Haskell,
reverse :: [a] -> [a]
reverse = reverse' []
where reverse' :: [a] -> [a] -> [a]
reverse' result [] = result
reverse' result (x:xs) = reverse' (x:result) xs
我同意 ShreevatsaR 的观点,如果你不将辅助函数设置为顶级(或者更糟,将其放入导出列表中),那么它的名称是什么并不重要。 我倾向于调用辅助函数 f 和 g。
reverse :: [a] -> [a]
reverse = f []
where
f ys [] = xs
f ys (x:xs) = f (x:ys) xs
我只是对小函数使用这个命名方案(否则我不知道f指的是什么)。 话又说回来,你为什么要写大函数呢?
但是,如果您确实想导出“帮助器”函数,因为它可能对其他人有用,我会称其为:
reverseAccumulator
像 Haskell 的 zip 和 zipWith 一样。 但我不会称这些为“帮助器”函数,zipWith 只是一个通用函数,zip 是默认实现(可能是最常用的实现)。
I agree with ShreevatsaR, if you don't make the helper function top-level (or worse, put it in the export list), than it doesn't matter what its name is. I tend to call helper functions f and g.
reverse :: [a] -> [a]
reverse = f []
where
f ys [] = xs
f ys (x:xs) = f (x:ys) xs
I just use this naming scheme for small functions (otherwise I don't know what the f refers to). Then again, why would you ever write big functions?
However, if you do want to export your 'helper' function because it might be useful to others, I would call it:
reverseAccumulator
Like Haskell's zip and zipWith. But I wouldn't call those 'helper' functions, zipWith is just a generic function and zip is the default implementation (probably the one thats used the most).
I always use do_, like "do_compute" with "compute". I find it quite descriptive as it is effectively the part of the function that performs the action, whereas the "compute" that gets called needs to have a simple descriptive name for the outside world.
I tend to add "_recurse" to the end. So "reverse_recurse". Not sure where I got that from. I like leaving the base case function simple as you have in your example. It tends to be the "public" function and the fact that it uses a helper function to perform the iteration is irrelevant to the caller. In javascript I sometimes go so far as to hide the iterative function via a closure, to make it really clear it is not to be called directly.
I also agree with ShreevatsaR, in this example I would make the helper a private function.
For other cases where I need helper functions to be visible in the whole module, but not exported, I tend to prefix functions with '_'. Sure, there is the explicit exports statement but during development I tend to export all functions to ease interactive exploration, e.g. in ghci. Later on I add the list of exported functions and the underbar makes it easy to remember whether I intended a function to be local or not.
发布评论
评论(7)
您可以随意调用辅助函数,只要不将辅助函数放在“全局”命名空间中就没有关系。 简单地添加“素数”似乎是一种常见的做法。 :) 例如,在 Haskell 中,
You can call the helper function anything you want, and it won't matter as long as you don't put the helper function in the "global" namespace. Simply adding a "prime" seems a common practice. :) E.g., in Haskell,
我同意 ShreevatsaR 的观点,如果你不将辅助函数设置为顶级(或者更糟,将其放入导出列表中),那么它的名称是什么并不重要。
我倾向于调用辅助函数
f
和g
。我只是对小函数使用这个命名方案(否则我不知道
f
指的是什么)。 话又说回来,你为什么要写大函数呢?但是,如果您确实想导出“帮助器”函数,因为它可能对其他人有用,我会称其为:
像 Haskell 的
zip
和zipWith
一样。但我不会称这些为“帮助器”函数,
zipWith
只是一个通用函数,zip
是默认实现(可能是最常用的实现)。I agree with ShreevatsaR, if you don't make the helper function top-level (or worse, put it in the export list), than it doesn't matter what its name is.
I tend to call helper functions
f
andg
.I just use this naming scheme for small functions (otherwise I don't know what the
f
refers to). Then again, why would you ever write big functions?However, if you do want to export your 'helper' function because it might be useful to others, I would call it:
Like Haskell's
zip
andzipWith
.But I wouldn't call those 'helper' functions,
zipWith
is just a generic function andzip
is the default implementation (probably the one thats used the most).我总是使用 do_,就像“do_compute”和“compute”一样。 我发现它非常具有描述性,因为它实际上是执行操作的函数的一部分,而被调用的“计算”需要对外部世界有一个简单的描述性名称。
I always use do_, like "do_compute" with "compute". I find it quite descriptive as it is effectively the part of the function that performs the action, whereas the "compute" that gets called needs to have a simple descriptive name for the outside world.
我使用
aux
或foo_aux
(用于主函数foo
),并嵌套定义,使其在外部不可见。I use
aux
orfoo_aux
(for main functionfoo
), and nest the definition so it's not externally visible.我倾向于在末尾添加“_recurse”。 所以“reverse_recurse”。 不知道我从哪里得到的。 我喜欢让基本情况函数像您的示例中那样简单。 它往往是“公共”函数,并且它使用辅助函数来执行迭代这一事实与调用者无关。 在 javascript 中,我有时甚至通过闭包隐藏迭代函数,以使其真正清楚地表明它不会被直接调用。
I tend to add "_recurse" to the end. So "reverse_recurse". Not sure where I got that from. I like leaving the base case function simple as you have in your example. It tends to be the "public" function and the fact that it uses a helper function to perform the iteration is irrelevant to the caller. In javascript I sometimes go so far as to hide the iterative function via a closure, to make it really clear it is not to be called directly.
我也同意 ShreevatsaR 的观点,在这个例子中,我会将助手设为私有函数。
对于其他情况,我需要辅助函数在整个模块中可见,但不导出,我倾向于在函数前加上“_”前缀。 当然,有明确的导出声明,但在开发过程中我倾向于导出所有函数以方便交互式探索,例如在 ghci 中。 后来我添加了导出函数的列表,并且下划线可以很容易地记住我是否打算将函数作为本地函数。
I also agree with ShreevatsaR, in this example I would make the helper a private function.
For other cases where I need helper functions to be visible in the whole module, but not exported, I tend to prefix functions with '_'. Sure, there is the explicit exports statement but during development I tend to export all functions to ease interactive exploration, e.g. in ghci. Later on I add the list of exported functions and the underbar makes it easy to remember whether I intended a function to be local or not.
设置并执行
示例:
setup and execute
example: