Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

int32 (6174)

int32
  (email not shown publicly)
+ -

  Comment: Re:unfoldr (Score 1) on 2009.01.29 17:42

by int32 on 2009.01.29 17:42 (#67110)
Attached to: A simple `unfold` in Perl
Oh well, I could say the same, read my post; I addressed that concern too :) unfoldr actually allows to return empty lists. Here's a hacked quote from haskell description:

The unfoldr function is a `dual' to foldr: while foldr reduces a list to a summary value, unfoldr builds a list from a seed value. The function takes the element and returns empty list if it is done producing the list or returns (@a,$b), in which case, @a is a appended to the list and $b is used as the next element in a recursive call.

As to the problem as you address it that you need to return two lists, I don't think there is a point to do so, at least for unfoldr, because it transforms a scalar to a list, not a list to a list. A list and a scalar is just fine.

Read More 10 comments
Comments: 10
+ -

  Comment: unfoldr (Score 1) on 2009.01.29 16:44

by int32 on 2009.01.29 16:44 (#67108)
Attached to: A simple `unfold` in Perl
I played a bit with the concept, and found an issue I can't resolve, possibly you can help me here. The problem concerns whether $_ is an alias or a copy to the parameter passed. If it is a copy then you can't modify it unless it is a reference. If it is an alias you can't modify it if it is read-only. You may ask why should it be modified at all, and here comes the classic unfoldr:

unfoldr { $_ ? ($_, $_ - 1) : () } 10;

10,9,8,7,6,5,4,3,2,1

which allows to pass arbitrary values to the callback . Now back to your example, if I want to send values other than the original parameter to myself (e.g. callback), then I can't. Also, I can't send an "undef" back to me that way. That seems too much of a limitation.

So a question. Why not a good old unfoldr?

unfoldr { @$_ ? ( pop @$_, $_ ) : () } [1..10];

10,9,8,7,6,5,4,3,2,1

Read More 10 comments
Comments: 10