Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^15: Near-free function currying in Perl

by BrowserUk (Patriarch)
on Nov 22, 2004 at 07:05 UTC ( [id://409509]=note: print w/replies, xml ) Need Help??


in reply to Re^14: Near-free function currying in Perl
in thread Near-free function currying in Perl

I did warn you that I was going to talk as if I understoof FP :) Thanks for making allowances and not making capital from that gross misassumption.

I remember when I first tried to learn SmallTalk. I was trying to get to grips with it at home whilst continuing to write hardcore C + assembler(device drivers) for 10 hours a day at work. I never quite "got" SmallTalk until I was commisioned to write a "First Introduction" course, and got to spend a month completely immersed in the language. Only then did the penny drop. It's still my all-time favorite language, although I have considerable reservations about it's use for commercial, multi-programmer projects.

It seems obvious that in order to get the usefulness of FP, I will need to spend a similar amount of time, or more, completely immersed in Haskell or one of the other modern implementations of FP. That is going to be a hard thing to pursuade myself to do. I have several ongoing projects in Perl and D that I want to bring to a close first.

**This is (mostly) a joke**.

A like your Fishing Pole analogy--a lot--but both times you brought it up, I couldn't help myself wondering why I (you) would want to use Perl to write FP?

I guess I will need to reach the point where writing code in the FP style no longer seems an "unnatural practice" before I will be able to answer that question.

When I do try to do something in Haskell, I still find myself having to spend a ratio of like 10 or 20 to 1 reading the docs and example code to time actually coding. I keep wanting to use loops, conditionals and temporary variables and insert a few prints to debug the code when it just sits there and does nothing. I started playing around with the famously elegant Haskell definition of the QuickSort algorithm. Neat, but is it ever slow and inefficient.

So thanks for your articles and the time you've taken responding to my questions and doubts. I'll shut up now and let you get on with your explorations of FP in Perl unhindered.


Examine what is said, not who speaks.
"But you should never overestimate the ingenuity of the sceptics to come up with a counter-argument." -Myles Allen
"Think for yourself!" - Abigail        "Time is a poor substitute for thought"--theorbtwo         "Efficiency is intelligent laziness." -David Dunham
"Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon
  • Comment on Re^15: Near-free function currying in Perl

Replies are listed 'Best First'.
Re^16: Near-free function currying in Perl
by tmoertel (Chaplain) on Nov 22, 2004 at 16:04 UTC
    Let me answer this question:
    A like your Fishing Pole analogy--a lot--but both times you brought it up, I couldn't help myself wondering why I (you) would want to use Perl to write FP?
    I want to be able to write FP-style code in Perl because it gives me more options for solving problems.

    I don't want to make Perl the next great FP language. All I want is to reduce the cost of FP in Perl so that I have the option to use FP when it's the best way to solve a problem.

    Cheers,
    Tom

      I've hoped you would respond with some examples of how you would use this in your own code. Could you show us examples with and without it? Or at least highlight what would be different?
        I'll provide some examples, but in order to appreciate them you must understand the library of tiny FP functions that form the core vocabulary of FP programming. I have enclosed a small subset of that vocabulary below, mostly drawn directly from the Haskell Prelude. I have tried to select examples that can be shown using only this subset.

        Keep in mind that the following code is probably too small, too simple, and too limited to be useful for drawing general conclusions. The best you can hope for is to get a taste. To draw inferences about the whole cuisine from this tiny sampling would be a mistake.

        First some library functions:

        Now let us write code using the above vocabulary. First, the preliminaries:

        use ToolBox; use Data::Dumper; # a convenience function for examining our output sub say(@) { local $Data::Dumper::Terse = 1; local $Data::Dumper::Indent = 0; my @d = map Dumper($_), @_; print "@d$/"; }
        Let us start with some simple functions to show the general idea:
        # some simple binary operators sub plus { $_[0] + $_[1] } sub times { $_[0] * $_[1] } # build n-ary operators from them *sum = foldl_c( \&plus, 0 ); *product = foldl_c( \&times, 1 ); # test them out say sum(1..10); # 55 say product(1..10); # 3628800
        Now let us build further to create a function to compute vector dot products:
        *dot_product = compose( \&sum, zip_with_c( \&product )); say dot_product( [1,1,1], [1,2,3] ); # 6
        So far, we have computed only with numbers. Let us now turn to data. One common programming task is to compute the combinations that can be generated by taking one element from a set of sets. (I selected this problem because various implementations can be found on Perl Monks for comparison.) Here's my implementation:
        *combos = foldr_c( \&outer_prod, [[]] ); sub outer_prod { my ($xs, $ys) = @_; [ map do { my $x=$_; map [$x, @$_], @$ys }, @$xs ]; } say combos( ['a','b'], [1,2,3] ); # [['a',1],['a',2],['a',3],['b',1],['b',2],['b',3]]
        Building further, let's compute power sets. One common method of computing the power set of a set S is the following:
        1. Replace each element e of S with the set {{e},{}}:
          [1,2] ==> [ [[1],[]], [[2],[]] ]
        2. Compute the combinations that can be formed from the sets:
                ==> [ [[1],[2]], [[1],[]], [[],[2]], [[],[]]] ]
        3. Compute the union of the sets within each combination:
                ==> [ [1,2], [1], [2], [] ]
        This method translates directly into the following code:
        *powerset = pipeline( map_c { [ [$_], [] ] }, # step 1 \&combos, # step 2 map_c { map [ map @$_, @$_ ], @$_ } # step 3 ); say powerset( 1, 2 ); # [1,2] [1] [2] [] say powerset(qw( a b c )); # ['a','b','c'] ['a','b'] ['a','c'] ['a'] # ['b','c'] ['b'] ['c'] []
        Each step in the original method maps directly to a stage in the composition pipeline, the output of each stage becoming the input of the next.

        This ends my example.

        You are welcome to code your own versions of these functions for comparison. Because these functions are so simple, the comparisons might not yield much insight. A more useful exercise might be for you to write FP and non-FP code to solve more complex problems and then compare the coding experiences instead of the code itself.

        Cheers,
        Tom

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (6)
As of 2024-04-16 17:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found