Is unfoldr too strict?

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

Is unfoldr too strict?

Isaac Elliott
I was reading this article https://wiki.haskell.org/Correctness_of_short_cut_fusion on the Haskell wiki. It presents an example (2.1.2) where destroy/unfoldr fusion behaves oddly. If I use a lazier definition of unfoldr, then this problem goes away:

unfoldr :: (b -> Maybe (a, b)) -> b -> [a]
unfoldr f b =
  case f b of
    Nothing -> []
    Just z -> fst z : unfoldr f (snd z)

Does this mean unfoldr is 'too strict'? Or is there a good reason for not writing it this way (performance, perhaps?) 

_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
Reply | Threaded
Open this post in threaded view
|

Re: Is unfoldr too strict?

Vanessa McHale

I think the "official" version could be implemented with a lazy pattern match and it'd be the same as yours, no?

Cheers,
Vanessa

On 12/11/18 12:55 AM, Isaac Elliott wrote:
I was reading this article https://wiki.haskell.org/Correctness_of_short_cut_fusion on the Haskell wiki. It presents an example (2.1.2) where destroy/unfoldr fusion behaves oddly. If I use a lazier definition of unfoldr, then this problem goes away:

unfoldr :: (b -> Maybe (a, b)) -> b -> [a]
unfoldr f b =
  case f b of
    Nothing -> []
    Just z -> fst z : unfoldr f (snd z)

Does this mean unfoldr is 'too strict'? Or is there a good reason for not writing it this way (performance, perhaps?) 

_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.

_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.

signature.asc (499 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Is unfoldr too strict?

Isaac Elliott
Yep, a lazy pattern match gets you the same benefit.

On Tue, 11 Dec. 2018, 7:50 pm Vanessa McHale, <[hidden email]> wrote:

I think the "official" version could be implemented with a lazy pattern match and it'd be the same as yours, no?

Cheers,
Vanessa

On 12/11/18 12:55 AM, Isaac Elliott wrote:
I was reading this article https://wiki.haskell.org/Correctness_of_short_cut_fusion on the Haskell wiki. It presents an example (2.1.2) where destroy/unfoldr fusion behaves oddly. If I use a lazier definition of unfoldr, then this problem goes away:

unfoldr :: (b -> Maybe (a, b)) -> b -> [a]
unfoldr f b =
  case f b of
    Nothing -> []
    Just z -> fst z : unfoldr f (snd z)

Does this mean unfoldr is 'too strict'? Or is there a good reason for not writing it this way (performance, perhaps?) 

_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.

_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.