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

Jon Fairbairn jon.fairbairn at cl.cam.ac.uk
Sun Oct 29 10:54:49 EST 2006


On 2006-10-27 at 17:56+0200 "Josef Svenningsson" wrote:
> My argument for intercalate builds on the fact that intersperse seems
> to be the wrong abstraction. Recall that I supplied some evidence that
> intersperse is only used together with concat:
> http://www.google.com/codesearch?q=file%3A%5C.hs+intersperse&btnG=Search+Code

Mostly, not only.

> For me this suggests that intersperse is the wrong
> abstraction. The right one is intercalate.

I don't agree with the methodology... Firstly, while extant
code tells you how things have been used, it doesn't tell
you how they /should/ be used. Second, I suspect that the
majority of current Haskell code was written by novices -- I
don't mean anyone any disrespect, it's just that it takes
quite a long time to become an expert, the number of new
novices has been increasing rapidly and the people who have
known Haskell long enough to be experts probably nolonger
write so much code: they get their students to do it
:-). [How many of the cases you found should have been foldr
(.) id etc?]

> I know that many people will argue that intersperse is
> somehow more primitive or more natural than
> intercalate.

I don't know about natural. Looking back at the 25 year old
Ponder list_ops library, I find that I defined

  join_with filler l1 [] = l1
  join_with filler l1 l2 = l1 ++ filler ++ l2

and then

  flatten_with filler = foldr (join_with filler) []

which is another way of writing intercalate.

> As for being more primitive, that's not true. Both
> functions are interdefinable.

Right. So your motive is more complicated than it appeared
to me. I missed that first time round, sorry.

The problem we have is that 

   intersperse x == intercalate [x] . map (:[])

and 

   intercalate x = concat . intersperse x

While it seems easier to me to work out the second than the
first, I don't know if that is simply due to habituation. I
can add that the second /looks/ simpler. So I /suspect/
(unprovably) that were intercalate the preferred form,
people would write intersperse in several different ways, in
contrast to the present state of affairs where “concat
. intersperse splod” seems already to have become idiomatic.

 What would convince me of your case would be someone saying
that intercalate is the <oojit> of the <weeble> category
while intersperse enjoys no such property...

... or maybe I was right back then, and the natural
primitive is something like join_with -- an operation to use
with foldr?


-- 
Jón Fairbairn                              Jon.Fairbairn at cl.cam.ac.uk






More information about the Libraries mailing list