Why is scanr strict in its third argument?

Geraint Jones Geraint.Jones at wolfson.ox.ac.uk
Thu Nov 9 16:04:44 UTC 2017

There are two things you might think of when you think of scanr;
or rather, there are two things I think of: a specification

	scanr f e = map (foldr f e) . tails

and an implementation

	scanr f e = foldr g [e] where g x yss@(ys:_) = f x ys : yss

Of course these two differ, because the one I called an implemntation
is strict wheras the specification isn't.

I wouldn't mind, but scanl goes to the trouble of not being strict,
as does Data.List.tails, which means inter alia that scanr (:) [] is
not an implementation of Data.List.tails as you might have expected.
(Well, as I did.)

In the context of

> arb = error "not used in demo"
> bot = error "bottom"

you get

*Main> (null . map (foldr arb arb) . Data.List.tails) bot
*Main> (null . scanr arb arb) bot
*** Exception: bottom
*Main> (null . scanl arb arb) bot

More information about the Glasgow-haskell-users mailing list