Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

When would you use functional programming?

by Ovid (Cardinal)
on Oct 16, 2002 at 18:50 UTC ( #205797=perlmeditation: print w/ replies, xml ) Need Help??

Help for this page

Select Code to Download


  1. or download this
    my @list = qw/ 7 5 2 2 /;
    my $prod = shift @list;
    $prod *= $_ foreach @list;
    
  2. or download this
    # bad.  Alters @_
    sub head { shift }
    
    # good.  Preserves @_
    sub head { $_[0] }
    
  3. or download this
    i = j + 1
    j = 6
    
  4. or download this
    prodList lst =
        if (length lst)==1 then head lst
    else head lst*(prodList (tail lst))
    
  5. or download this
    sub head { $_[0] }
    sub tail { @_[1..$#_] }
    ...
        ? head @_ 
        : (head @_) * prod_list( tail @_ ) 
    }
    
  6. or download this
    sub factorial { prod_list( 1 .. $_[0] ) }
    
  7. or download this
    #!/usr/bin/perl -w
    use strict;
    ...
    sub factorial { prod_list 1 .. $_[0] }
    
    print factorial shift;
    
  8. or download this
    sub factorial { 
      my $result = 1;
      $result   *= $_ for 1 .. $_[0];
      $result
    }
    
  9. or download this
    @new_list =
      map  { $_->[0] }
      sort { $a->[1] <=> $b->[1] }
      map  { [ $_, expensive_func( $_ ) ] }
        @old_list;
    
  10. or download this
    # who says Perl has too much punctuation? :)
    sub prod_list { 1 == @_ ? $_[0] : $_[0] * prod_list( @_[1..$#_] ) }
    

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://205797]
Approved by ajt
Front-paged by broquaint
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2014-08-23 07:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (172 votes), past polls