[Haskell] definition of intersperse

Daan Leijen daanleijen at xs4all.nl
Wed Mar 3 11:47:49 EST 2004


On Tue, 02 Mar 2004 16:59:48 -0800, Iavor S. Diatchki <diatchki at cse.ogi.edu> wrote:
> i'd like to suggest that the definition of "intersperse" from the List module be made more lazy.

Good thing, I think that library functions should always be as lazy as possible
in their observeable interface (or well documented why they aren't)

>> intersperse             :: a -> [a] -> [a]
>> intersperse _   []      = []
>> intersperse sep (x:xs)  = x : rest
>>   where rest [] = []
>>         rest xs = sep : intersperse sep xs

I don't like the generic name "rest" so much, and the function is not
as efficient as it could be due to too much matching -- what about:

> intersperse            :: a -> [a] -> [a]
> intersperse sep []     = []
> intersperse sep (x:xs) = x : prefix sep xs
>
> prefix            :: a -> [a] -> [a]
> prefix sep []     = []
> prefix sep (x:xs) = sep : x : prefix sep xs


-- Daan.



More information about the Haskell mailing list