http://qs1969.pair.com?node_id=908617


in reply to Re: (almost) foldl
in thread (almost) foldl

negative numbers?

Replies are listed 'Best First'.
Re^3: (almost) foldl (-map)
by tye (Sage) on Jun 08, 2011 at 19:45 UTC
    my @list = ( -4 .. 6 ); print map((1)x$_,@list) - map((1)x-$_,@list);

    - tye        

      Wow. That's a exactly (and I mean, exactly) the first idea I had to solve the negative numbers problem. I didn't post it, though, because it doesn't handle decimal numbers.
Re^3: (almost) foldl
by Grimy (Pilgrim) on Jun 08, 2011 at 10:07 UTC
    sub sum { eval 'pop(@_)+' x ($#_ + 1) . '0'; } @list = (-4, 15, -5, 2.5); print sum @list; # 8.5, as expected print sum -4, 15, -5, 2.5; # Works the same print sum; # 0 (not undef)
    EDIT: replaced $#list with $#_ , so that it works with lists of any length. Silly me.