http://qs321.pair.com?node_id=750246

I needed a scan function for some data processing I am doing. Unfortunately Sub::Curried does not work, so I cannot use it's scan function.

Language::Functional has a broken test suite. And I can't figure out how to fix its issues.

fp does not have a scanl function. I considered contributing this code to it, but am turned off by the semantics of the reduce function... I think that would be better named behead. It also potentially conflicts with List::Util in that respect.

So here we have the scanl function, a staple of functional programming, available in standalone fashion. As you can see, it behaves the same as scanl for haskell

use strict; use warnings; use Data::Dumper; =for comment scanl f q ls = q : case ls of [] -> [] x:xs -> scanl f (f q x) xs =cut sub scanl { my ($f, $seed, @list)=@_; my @tail = @list[ 1 .. $#list ] ; my @rest = scalar @list ? scanl($f, $f->($seed, $list[0]), @tail) +: (); ($seed, @rest); } my @lis = (4,2,4); sub add { $_[0] + $_[1] } sub divide { $_[0] / $_[1] } my @result = scanl \&divide, 64, @lis; warn Dumper(\@result); @result = scanl \&divide, 3, (); warn Dumper(\@result); use List::Util 'max'; @result = scanl \&List::Util::max, 5, (1 .. 7) ; warn Dumper(\@result);