Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re^2: (almost) foldl

by dk (Chaplain)
on Jun 08, 2011 at 04:56 UTC ( #908617=note: print w/replies, xml ) Need Help??

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.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://908617]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2022-08-15 06:21 GMT
Find Nodes?
    Voting Booth?

    No recent polls found