[Haskell-beginners] Re: Integer factorization

Heinrich Apfelmus apfelmus at quantentunnel.de
Sun Mar 15 06:24:03 EDT 2009


Francesco Bochicchio wrote:
> Heinrich Apfelmus wrote:
>>
>> Abstraction is the one driving force for very short names. For example,
>> take the definition of  foldr
>>
>>   foldr f z []     = z
>>   foldr f z (x:xs) = f x (foldr f z xs)
>>
>> Since this function is polymorphic, so f , z  and the  xs  can be
>> anything, using more "descriptive" variable names is simply not
>> possible; the key point of fold is its generality.
> 
> Ok but one could still hint at their structure or purpose:
> 
> foldr function value (x:xs) =  function x ( foldr function value xs )
> 
> I believe this would give a little more information to the casual reader.

Sure, though there is already the convention from mathematics that
functions are named  f, g  and values are denoted by  x, y or z  .

> I have some resistance to use nouns for functions. In the imperative world,
> nouns are for variables, verbs are for functions.
> I know that in pure functional programming there is not such a thing
> as variables, but still I would reserve nouns for function parameters and
> bound expressions. Hence if I have a function that
> find factors, I would call it findFactors rather than just factors.
> 
> One such example of misnaming - from a beginner point of view -  is the
> length function in prelude: if it was called
> count, I believe more beginners would have realized that works by actually
> counting the elements of
> a list and not  by accessing to some already available 'property' of the
> list.

IMHO, I think that using nouns like  length  or  factors  is a good
idea, for they sounds nice when used in compound expressions. Compare
for instance

   take (length xs `div` 2) xs

   take (count  xs `div` 2) xs

The first can be put into pseudo-english as

   "take (the length of xs divided by 2) elements of xs"

while the verb in the second makes it difficult to use the expression in
parenthesis as object to another verb. In contrast, the runtime (O(n)
versus O(1) for say a record field) is rather secondary.

Implicitly adding the preposition "of" makes the nouns flow, i.e.
"factors of n", "length of xs".


>> Convention. Often, an auxiliary function that does basically the same
>> thing as the main function  factors  but with an extra parameter will be
>> named  factors' . The apostrophe has the drawback that it's easy to
>> forget, so some people now name such auxiliary functions  go  instead.
>>
>>
> I tend to use _ instead of '. Is more visible and keep conveying the idea
> that the auxiliary function is just a slight variation of the main one.

The convention of using an apostrophe for variations comes from
mathematics and it seems that it was put into the Haskell syntax
specifically for this purpose.



In any case, the rationales and style that I presented are how I
interpret the naming conventions from classic papers like

    John Hughes. Why functional programming matters.
    http://www.cs.chalmers.se/~rjmh/Papers/whyfp.pdf

    John Hughes. The Design of a Pretty-Printing Library
    http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.38.8777

Most of these conventions root in mathematics, of course. I like them
for they encourage thinking purely functionally, but these are of course
not the only possibilities.


Regards,
apfelmus

--
http://apfelmus.nfshost.com



More information about the Beginners mailing list