Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat makes a good name for a helper function?
    primarykey
    data
    text
    <p>Consider the following problem: given a list of length three of tuples (String,Int), is there a pair of elements having the same "Int" part? (For example, <code>[("bob",5),("gertrude",3),("al",5)]</code> contains such a pair, but <code>[("bob",5),("gertrude",3),("al",1)]</code> does not.)</p> <p>This is how I would implement such a function:</p> <pre><code>import Data.List (sortBy) import Data.Function (on) hasPair::[(String,Int)]-&gt;Bool hasPair = napkin . sortBy (compare `on` snd) where napkin [(_, a),(_, b),(_, c)] | a == b = True | b == c = True | otherwise = False </code></pre> <p>I've used pattern matching to bind names to the "Int" part of the tuples, but I want to sort first (in order to group like members), so I've put the pattern-matching function inside a <code>where</code> clause. But this brings me to my question: <strong>what's a good strategy for picking names for functions that live inside <code>where</code> clauses</strong>? I want to be able to think of such names quickly. For this example, "hasPair" seems like a good choice, but it's already taken! I find that pattern comes up a lot - the natural-seeming name for a helper function is already taken by the outer function that calls it. So I have, at times, called such helper functions things like "op", "foo", and even "helper" - here I have chosen "napkin" to emphasize its use-it-once, throw-it-away nature.</p> <p>So, dear Stackoverflow readers, what would you have called "napkin"? And more importantly, how do you approach this issue in general?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload