“foop”: a naming convention? It's a helper recursive function for “foo”; what does the suffix “p” mean?

北城以北 提交于 2019-12-12 16:02:54

问题


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?.)


回答1:


In this case, I believe p stands for "prime". Rather than calling the helper choose' or chooseprime, they use choosep.




回答2:


I think it stands for 'prime' -- in OCaml, which allows ' in identifiers, helper functions are frequently named foo'. 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.




回答3:


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 '?'.




回答4:


P stands for 'predicate'. A thing that returns 'true' or 'false'.



来源:https://stackoverflow.com/questions/5279286/foop-a-naming-convention-its-a-helper-recursive-function-for-foo-what-do

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!