Why is scanr strict in its third argument?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Why is scanr strict in its third argument?

Geraint Jones
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
False
*Main> (null . scanr arb arb) bot
*** Exception: bottom
*Main> (null . scanl arb arb) bot
False
_______________________________________________
Glasgow-haskell-users mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/glasgow-haskell-users
Reply | Threaded
Open this post in threaded view
|

Re: Why is scanr strict in its third argument?

David Feuer
I think this discussion would be more appropriate to the libraries list.

On Nov 9, 2017 11:05 AM, "Geraint Jones" <[hidden email]> wrote:
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
False
*Main> (null . scanr arb arb) bot
*** Exception: bottom
*Main> (null . scanl arb arb) bot
False
_______________________________________________
Glasgow-haskell-users mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/glasgow-haskell-users

_______________________________________________
Glasgow-haskell-users mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/glasgow-haskell-users