[erlang-questions] Proposal: add lists:intersperse/2 and lists:intercalate/2
Richard A. O'Keefe
ok@REDACTED
Thu Mar 3 01:32:13 CET 2016
On 3/03/16 3:47 am, Jesper Louis Andersen wrote:
> I'd really like to add two functions to the lists module from Haskell:
>
> intersperse(List, Seperator)
Why have you swapped the arguments? The Haskellfunction
Data.List.intersperse :: a -> [a] -> [a]
puts the separator *first*.
> produces a list where each element is separated by separator,
You can't separate one thing. The elements *are* separated by the
separator.
>
> and it's cousin, intercalate(ListOfLists, Separator) is
> append(intersperse(ListOfLists, Seperator)),
Once again, why have you switched the arguments?
Data.List.intercalate :: [a] -> [[a]] -> [a]
puts the separator *first*.
I've never really been happy with the name "intercalate".
To the extent that it's acceptableto use "intercalate"
for anything other than adding chunks to the calendar in
order to fix itup, it means what the intersperse function
does. However, the name _is_ established inHaskell, and
there's no point in gratuitous difference.
Which is why it is important to get the argument order right.
intersperse and intercalate are very nearly the same function.
intersperse sep xs = interwhatsit (:) [] (sep:) xs
intercalate sep xs = interwhatsit (++) [] (sep++) xs
interwhatsit :: (a -> b -> b) -> b -> (b -> b) -> [a] -> b
-- Combine End Separate Items
interwhatsit _ e _ [] = e
interwhatsit c e s (x:xs) = loop xs x
where loop [] x = c x e
loop (y:ys) x = c x $ s $ loop ys y
Do we want to have the special cases without the general case?
(There has to be a better name than interwhatsit. sepfoldr?)
>
> The rationale for this proposal is that I find myself implementing
> this function again and again in every project I write
Why would you do that rather than putting it in a utilities module and
just reusing that module?
> , and it is highly generic.
That sounds as though you have some other use in mind for these
functions than pasting strings together. I'd love to know what.
I've been using Haskell since before it had intersperse and
intercalate, and can't remember ever using them.
More information about the erlang-questions
mailing list