Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Functional programming with Perl

by spurperl (Priest)
on Dec 01, 2002 at 13:36 UTC ( [id://216758]=perlmeditation: print w/replies, xml ) Need Help??

Hello fellow Perl monks, I'm very interested in the Lisp programming language. As such, I've seen quite a few discussions in LISP related forums. One that comes up often is that modern languages (Python, Perl, etc...) are coming closer and closer to Lisp, employing functional programming tactics. Just to remind you:

"Functional programming is a style of programming that emphasizes the evaluation of expressions rather than the execution of commands."

I've came to think, what reminds of this style in Perl. Here are some examples that immediately popped up in my head:

Mapping

@arr = (1, 2, 3, 4, 5); map {print "** $_ **\n";} @arr;

Anonymous subs & treating subs as data

$ast_print = sub {print "** $_[0] **\n"}; process_and_print(25, $ast_print); sub process_and_print { my $val = $_[0]; my $print_func= $_[1]; #..... processing .... &$print_func($val); }

Eval: evaluating strings as Perl expressions

$op = "print \"hello\""; eval $op;

The latter reminds me of a nice example the Lisp people like to show. How do you write a Lisp interpreter ? Easy:

(loop (print (eval (read))))

You can do the same in Perl, and it isn't less easy (though a bit more obfuscated - the Perl way :)

while (<>) { eval; print $@; }

Note: all of the above are toy examples, chosen to demonstrate their specific purposes.

And I wanted to consult you, knowledgeable monks, what other examples do you have in mind ? Some very cool things may be done with the constructs shown above. eval is frequently used in obfuscations, but it is a very powerful tool - for code reading and executing code, etc.

Replies are listed 'Best First'.
Re: Functional programming with Perl
by adrianh (Chancellor) on Dec 01, 2002 at 14:09 UTC
Re: Functional programming with Perl
by gjb (Vicar) on Dec 01, 2002 at 15:57 UTC

    You might want to look into this article that talks about Perl and the lambda calculus (which is the mathematical foundation of functional programming).

    Just my 2 cents, -gjb-

Re: Functional programming with Perl
by hawtin (Prior) on Dec 01, 2002 at 21:41 UTC

    Just to be a tad pedantic Lisp is not a functional programming language. A functional language has no side effects (and a purely functional language can be great fun to program). For an overview of the difference how about this. Many of the neat features of Lisp can best be explored in dialects such as scheme which has certainly been one of the influences in Perl's design (e.g. closures)

    If you really want to explore language design then you could try doing all the examples in Abelson and Sussman in Perl (I am sure it is possible and it would certainly be both interesting and a learning experience)

      Lisp's general model is functional. It makes it easier for you to write in functional style, and is lots of fun. To be more practical, it allows assignments and other side effects, but their usage is discouraged, except for in some specially defined cases (global counters, for instance, which would be quite tedious w/o side effects).

      Abelson and Sussman's book is terrific. As a part of a course, I did quite a lot of exercises in it, in Scheme and it was great.

        Says spurperl:
        Lisp's general model is functional.
        It isn't.
        it allows assignments and other side effects, but their usage is discouraged
        Perhaps by you, but not by any serious Lisp programmers that I know. What do you think CLOS is for? Do you think that it was invented so that its use could be discouraged? I suggest you read Common Lisp: The Language by Steele or On Lisp by Graham, and perhaps revise your opinion.

        --
        Mark Dominus
        Perl Paraphernalia

      Abelson and Sussman is available online for those who are interested and like online books/don't want to spend money on the book.

      Hope this helps, -gjb-

      That's a rather restrictive definition for a functional language (e.g. it would exclude ML) - I'd prefer to call something completely side-effect free a "purely functional language", and reserve the term "functional language" for one that strongly encourages one to program functionally (to be inclusive of the like of ML), and to then say that something like Common Lisp that makes functional programming easy to do but doesn't really favor it over other styles of programming is a language that "supports" functional programming.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2025-06-17 17:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.