Names for small functions: just say no... Re: Data.List.join

Jon Fairbairn jon.fairbairn at cl.cam.ac.uk
Fri Nov 10 11:27:31 EST 2006


On Thu, 9 Nov 2006 17:27:39 +0000  you wrote:
> Jon Fairbairn <jon.fairbairn at cl.cam.ac.uk> wrote:
> 
> > Then there's the specific question of whether intercalate is
> > actually a better abstraction than intersperse.
> > "concat . intersperse" is hardly difficult to read,
> 
> I have found over the years, that pretty-much every time I write
> "intersperse" in a program, I get a type error.  It almost always turns
> out that I really wanted "concat . intersperse", and so I write that
> instead.

That is certainly an interesting datapoint. Unfortunately I
don't believe I've ever made that particular mistake, so we
cancel each other out ;-).  But it raises a point about
collecting data for this kind of argument. What we want is a
history of the mistakes people made; /then/ we could argue
from searches in such histories which constructs were more
error-prone. Until you wrote this I was thinking that
perhaps a search of version-control histories would do, but
since people don't (I hope!) check in code that doesn't
compile, your sort of problem goes unrecorded.

> So perhaps this just demonstrates that I have the wrong
> idea about what intersperse is supposed to do.  But the
> very fact that I keep making the same mistake suggests
> that there is indeed a natural and intuitive function
> hiding there, and I keep reaching for it, but with the
> wrong name.

I suspect what's natural and intuitive differs from person
to person too much for us to make progress on that front
without the sort of data that I say above is hard to get,
which is why I'd prefer to see arguments about mathematical
properties. 


Largely pointless rumination follows.

One of the properties is the type; I can immediately think
of three types that the "intuitive" function might have:

intersperse :: a -> [a] -> [a]
inter_concat :: [a] -> [a] -> [a] -- not the best name?
intercalate :: [a] -> [[a]] -> [a]

from that I should prefer intersperse on the grounds that it
has a simpler type, but when I tried writing each in terms
of the other, I found that the definitions of intersperse
and intercalate in terms of inter_concat were both nice and
obvious. Defining inter_concat in terms of intersperse is a
bit messy, and inter_concat in terms of intercalate only
slightly less so, which bend me towards the belief that
inter_concat is the most primitive!

The only conclusion I can really draw from this is that the
argument that wins for me is still: intersperse got there
first, it may not be ideal, but the grounds for adding
another are too weak.

   Jón


-- 
Jón Fairbairn                                 Jon.Fairbairn at cl.cam.ac.uk
31 Chalmers Road                                         jf at cl.cam.ac.uk
Cambridge CB1 3SZ            +44 1223 570179 (after 14:00 only, please!)




More information about the Libraries mailing list