Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
#!/usr/bin/perl -w # A non-recursive left fold (foldl), taken from Language::Functional sub foldl(&$$) { my($f, $z, $xs) = @_; map { $z = $f->($z, $_) } @{$xs}; return $z; } # Recursive foldl sub foldl_rec { my($f, $z, $xs) = @_; my($head, @tail) = @$xs; $head ? foldl_rec($f, $f->($z,$head), \@tail) : $z; } # "Fold" is the universal list traversal function. Also known as # "reduce" (see List::Util) and "accumulate" (C++ STL). Any function # you write that munges lists (map, grep, etc.) can be rewritten in # terms of a fold. It essentially takes a list and replaces each "con +s" # constructor with a function. Stated another way, if you have a list # @a = (1, 2, 3, 4), fold will replace the commas with another functio +n # of your choosing. Let's say you want the sum of the elements in @a. # Replace the commas with a '+' sign, (1 + 2 + 3 + 4). Easy isn't it? # You might write it as... $s = foldl(sub{ $_[0] + $_[1] }, 0, [1..4]); print "sum = $s\n"; # 10 # addition to providing the function and the list, you supply an # initial value to start out with. In the case of $sum above, we use # 0. If you want the product of the elements in the list you can chan +ge # to... $p = foldl(sub{ $_[0] * $_[1] }, 1, [1..4]); print "product = $p\n"; # 24 # The "left" portion comes into play because we start at the left end +of # the list and work towards the right. The actual sum that is # calculated is (((((0+1)+2)+3)+4). It only makes a difference when t +he # function used isn't associative. Subtraction is an example... $l = foldl(sub{ $_[0] - $_[1] }, 0, [1..4]); print "left fold subtraction = $l\n"; # ((((0-1)-2)-3)-4) == -10 # Recursive foldr sub foldr_rec { my($f, $z, $xs) = @_; my($head, @tail) = @$xs; $head ? $f->($head,foldr_rec($f, $z, \@tail)) : $z; } $r = foldr_rec(sub{ $_[0] - $_[1] }, 0, [1..4]); print "right fold subtraction = $r\n"; # (1-(2-(3-(4-0)))) == -2 # The dual of "fold" is the universal list creation function, "unfold" +. # See more unfold in action... # #

In reply to Re^7: Derangements iterator (callbacks) by Anonymous Monk
in thread Derangements iterator by tye

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others romping around the Monastery: (5)
    As of 2018-10-20 01:44 GMT
    Find Nodes?
      Voting Booth?
      When I need money for a bigger acquisition, I usually ...

      Results (111 votes). Check out past polls.

      • (Sep 10, 2018 at 22:53 UTC) Welcome new users!