Reid Barton | 16 Aug 16:49
Favicon

laziness of intersperse

(This is the same issue as http://www.haskell.org/pipermail/haskell/ 
2004-March/013739.html but there was no follow-up at that time.)

The intersperse library function is not as lazy as it could be.  The  
current definition of intersperse is

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

For any list (x:xs) not containing _|_, intersperse sep (x:xs) is a  
list of the form (x:...); yet intersperse sep (x:_|_) = _|_ because  
the pattern match on the second equation diverges.  A better  
definition would be

intersperse _ [] = []
intersperse sep (x:xs) = x : intersperseWithPrefix xs
   where intersperseWithPrefix [] = []
         intersperseWithPrefix (x:xs) = sep : x :  
intersperseWithPrefix xs

(slightly modified from http://www.haskell.org/pipermail/haskell/2004- 
March/013741.html)

An application: There was a question on #haskell about how to compute  
the "ruler" sequence [1,2,1,3,1,2,1,4,1,2,1,3,1,2,1,5,...].  The  
definition

ruler = fix ((1:) . intersperse 1 . map (1+))
(Continue reading)

Gwern Branwen | 16 Aug 18:13

Re: laziness of intersperse

On 2008.08.16 10:51:14 -0400, Reid Barton <rwbarton <at> math.harvard.edu> scribbled 1.3K characters:
> (This is the same issue as http://www.haskell.org/pipermail/haskell/
> 2004-March/013739.html but there was no follow-up at that time.)
>
> The intersperse library function is not as lazy as it could be.  The
> current definition of intersperse is
>
> intersperse             :: a -> [a] -> [a]
> intersperse _   []      = []
> intersperse _   [x]     = [x]
> intersperse sep (x:xs)  = x : sep : intersperse sep xs
>
> For any list (x:xs) not containing _|_, intersperse sep (x:xs) is a list
> of the form (x:...); yet intersperse sep (x:_|_) = _|_ because the
> pattern match on the second equation diverges.  A better definition would
> be
>
> intersperse _ [] = []
> intersperse sep (x:xs) = x : intersperseWithPrefix xs
>   where intersperseWithPrefix [] = []
>         intersperseWithPrefix (x:xs) = sep : x : intersperseWithPrefix xs
>
> (slightly modified from http://www.haskell.org/pipermail/haskell/2004-
> March/013741.html)
>
> An application: There was a question on #haskell about how to compute
> the "ruler" sequence [1,2,1,3,1,2,1,4,1,2,1,3,1,2,1,5,...].  The
> definition
>
> ruler = fix ((1:) . intersperse 1 . map (1+))
(Continue reading)

Bart Massey | 16 Aug 19:22
Favicon
Gravatar

Re: laziness of intersperse

A cleaner but still fully lazy version of intersperse might be

    intersperse sep (x1 : x2 : xs) =
        x1 : sep : intersperse sep (x2 : xs)
    intersperse _ l = l

(I'd quote to give some context, but gmane says I can't unless I artificially
pad the length of my new text.  Is there any way to post to this list other than
gmane?)
Thomas Schilling | 17 Aug 01:42
Gravatar

Re: laziness of intersperse

On Sat, Aug 16, 2008 at 7:22 PM, Bart Massey <bart <at> cs.pdx.edu> wrote:
> A cleaner but still fully lazy version of intersperse might be
>
>    intersperse sep (x1 : x2 : xs) =
>        x1 : sep : intersperse sep (x2 : xs)
>    intersperse _ l = l

Doesn't that fail on (x:_|_) ?  Also it relies on constructor
specialisation to be efficient.

> (I'd quote to give some context, but gmane says I can't unless I artificially
> pad the length of my new text.  Is there any way to post to this list other than
> gmane?)

Subscribe?  http://www.haskell.org/mailman/listinfo/libraries
Ian Lynagh | 16 Aug 19:10
Gravatar

Re: laziness of intersperse


Hi Reid,

On Sat, Aug 16, 2008 at 10:51:14AM -0400, Reid Barton wrote:
> (This is the same issue as http://www.haskell.org/pipermail/haskell/ 
> 2004-March/013739.html but there was no follow-up at that time.)

Since then we have created the library submissions procedure:

http://www.haskell.org/haskellwiki/Library_submissions

If you follow that then everyone interested can have a say. We can then
make the change if there is a consensus for it, without the issue
getting forgotten about.

Thanks
Ian
Duncan Coutts | 16 Aug 18:29

Re: laziness of intersperse

On Sat, 2008-08-16 at 10:51 -0400, Reid Barton wrote:
> (This is the same issue as http://www.haskell.org/pipermail/haskell/ 
> 2004-March/013739.html but there was no follow-up at that time.)
> 
> The intersperse library function is not as lazy as it could be.  The  
> current definition of intersperse is
> 
> intersperse             :: a -> [a] -> [a]
> intersperse _   []      = []
> intersperse _   [x]     = [x]
> intersperse sep (x:xs)  = x : sep : intersperse sep xs
> 
> For any list (x:xs) not containing _|_, intersperse sep (x:xs) is a  
> list of the form (x:...); yet intersperse sep (x:_|_) = _|_ because  
> the pattern match on the second equation diverges.  A better  
> definition would be
> 
> intersperse _ [] = []
> intersperse sep (x:xs) = x : intersperseWithPrefix xs
>    where intersperseWithPrefix [] = []
>          intersperseWithPrefix (x:xs) = sep : x :  
> intersperseWithPrefix xs
> 
> (slightly modified from http://www.haskell.org/pipermail/haskell/2004- 
> March/013741.html)
> 
> An application: There was a question on #haskell about how to compute  
> the "ruler" sequence [1,2,1,3,1,2,1,4,1,2,1,3,1,2,1,5,...].  The  
> definition
> 
(Continue reading)


Gmane