in reply to Re^5: Specializing Functions with Currying
in thread Specializing Functions with Currying

Why recurse when you can iterate instead? :)

*gasp* .... thats not very functional! (I wont even bring up the fact you are using variable assignment, tssk tssk)

If we really want to be "efficient" about it, we would avoid the overhead of the wrapper subroutine, and just use straight perl's closures.

sub compose { my (@funcs) = @_; return sub { my (@args) = @_; foreach my $func (@funcs) { @args = $func->(@args); } return @args; }; }
But really, that takes all the fun out of it ;-)

-stvn

Replies are listed 'Best First'.
Re^7: Specializing Functions with Currying
by jryan (Vicar) on Aug 06, 2004 at 22:21 UTC

    Why be functional when you can be efficient? Recursion might be elegant, but computers don't really understand elegance. :) Functional programming has a lot of great and useful concepts in it, and luckily we use a language that doesn't *force* us to be purists about it.

    P.S.: The variable assignment is very necessary in my second example, because otherwise you're going to get a circularly referenced subroutine, which will make perl hit a infinite recursion warning pretty damned quick.

    P.P.S: And, you're right, your last version is even better!

      Actually programming languages that understand tail-recursion would have made stvn's solution both elegant and efficient.

      That said, some algorithms just cannot be efficiently implemented in a "pure" functional manner. A good example is the Sieve of Eratosthenes.

        Something I've noticed in some of my own (brief) studies into functional programming is that it tends to make one think in algorithms that are not always the most efficient, and sometimes tail-recursion won't save you.

        For instance, in a SoPW a while back, a poster wanted an efficient way to get the highest number in a list (or something along those lines). I posted this solution:

        my $highest = pop sort { $a <=> $b } @list;

        For some reason, my brain was convinced that this was the most efficient solution. While it may be a wonderful bit of functional code, it is not nearly as efficient as the iterative solution I knew quite well when I was a new C programmer:

        my $highest = pop @list; foreach (@list) { $highest = $_ if $_ > $highest; }

        Just what was I thinking?

        I guess this is a warning not to rely on any one tool too much.

        "There is no shame in being self-taught, only in not trying to learn in the first place." -- Atrus, Myst: The Book of D'ni.

      Why be functional when you can be efficient

      Spoken like a true Electrical Engineering Major ;-)

      Functional programming has a lot of great and useful concepts in it, and luckily we use a language that doesn't *force* us to be purists about it.

      I agree with you on both points. However, as tilly points out, recursion (and other functional programming goodies) don't have to be inefficient. I would encourage you to check out Standard ML, and in particular its compilation model (which I can't find any good links to at the moment, but I will post them when I find them). ML is a fast functional language, and according to the (inherently flawed) computer language shootout one of the fastest (faster than C++ in some cases). In addition there are some really fast LISP implementations out there too as well.

      Basically with a good compiler, you can be functional and efficient.

      -stvn

        Hey, don't take me wrong, I'm all about elegance. My argument isn't against functional langauges (hell, I used reduce, which is the same thing in lisp! lisp, baby! how's that for functional!), it was that recursion in Perl is crazy inefficient, and to use it just because its "functional" is kind of silly. :) Tilly's point was about tail-recursion, which is an optimization that Perl doesn't support (for some reason, I honesetly don't have a clue why).

Re^7: Specializing Functions with Currying
by ihb (Deacon) on Aug 08, 2004 at 00:19 UTC

    For full equivalence you need to not spoil the aliasing of @_ for the first call. Replace all @args with @_ and you're home safe. (The @_ assignment later breaks the aliasing as it should.)

    Cheers,
    ihb

    Read argumentation in its context!