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

Functional programming ?

by spx2 (Deacon)
on Apr 17, 2009 at 01:16 UTC ( #758107=perlquestion: print w/replies, xml ) Need Help??
spx2 has asked for the wisdom of the Perl Monks concerning the following question:

I think it's very nice to write as little code as possible, not to the point where no code at all is needed,but as little as possible. Recently I read a article about monads in Perl , which made a big impression on me. It expressed some things in a very elegant way. To what extent those things are useful is still an unanswered question for me.

I found myself rewinding on some old Perl presentation and finding about a regex to validate nested parens,this also was very elegant. Are there any other articles that describe what is covered in this presentation in a more detailed way ?(in particular Monads).

Have you used Monads in Perl and why would they be useful ? Have you used functional programming in Perl , to what extent and has it been useful or just a caprice?

Replies are listed 'Best First'.
Re: Functional programming ?
by BrowserUk (Pope) on Apr 17, 2009 at 03:48 UTC

    Imagine a van driver doing his days work. He starts off by loading his van with the days deliveries. Then he fuels up and progresses around his route delivering packages until he's done.

    Now with monads.

    Loads his van with the days deliveries. Drives to the gas station (with a new van following along behind). Puts fuel in the new van and transfers all the packages from the first van to the second. Drives to the first delivery and offloads the first package to the customer. Then tranfers all the remaining packages to another new van. Progresses to the second delivery and offloads a customer package and then transfers all the remaining packages to yet another new van. ...

    Cool huh!


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      I suspect that few understand monads well enough to get your analogy.

      The short explanation is that in pure functional programming, every time you call a function with a fixed set of arguments, you must get the same response back. But if you do I/O, you want it to do a different thing each time. Monads are used to solve this problem by creating a container that will be a different argument each and every time you call the function, which therefore makes it OK to get different results back. In the above analogy, therefore, the truck is the monad. And every time you make a delivery, you need a different truck.

      Monads can be used for other things as well, but this is the one that first confronts anyone learning Haskell.

      What happens with all the (now) empty vans? Does the driver has to keep them for his tomorrow deliveries or sell them at a profit or bring them all back to the garage or torch them, or ...?

      CountZero

      A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: Functional programming ?
by ELISHEVA (Prior) on Apr 17, 2009 at 10:38 UTC

    I don't know about monads, but one often useful reason for functional programming is deferred execution and simulation modeling. For example, I might need to set up a complex set of calculations at the start of the program. The calculation itself is designed at the start of the program based on user input. The outcome of the model relies on values that we will only know later on in the program.

    Here is a rather silly example, implementing a prototype of a child's algebra game. Even though we have only defined two operations (add, multiply), we can generate and solve problems for any polynomial equation the child wants to play with:

    A more real-life example might involve something that lets a user define a process flow. We compose the process flow using functions. Then later on we run the user composed process flow against a number of different real life inputs.

    Best, beth

Re: Functional programming ?
by morgon (Curate) on Apr 17, 2009 at 10:27 UTC
    Here is a good introduction to Monads.
Re: Functional programming ?
by dk (Chaplain) on Apr 17, 2009 at 13:58 UTC
    Reading about monads was the last straw for me back then when I was dissatisfied with the existing async I/O frameworks. Especially I didn't like it when I needed to implement process flows, that may consist of several orderless callbacks, such as on_read, on_timeout, etc etc, where it was too easy to lose control which callback is executed after which. Or, f.ex. trying to maintain a consistent timeout between the stages. Different stuff.

    Monads in particular gave me an insight about how I can force order in such a set of callbacks. As a result, I've done a yet another framework IO::Lambda, which borrows ideas heavily from functional programming (including monads too).

Re: Functional programming ?
by morgon (Curate) on Apr 17, 2009 at 14:55 UTC
    I have not thought too deeply on Monads in Perl but my impression is that you can only borrow some ideas and get an approximation to what Monads are in e.g. Haskell (the only programing language where they play a really prominent part).

    The reason for this is that Perl lacks the proper type-system, so you cannot properly type e.g. the "bind"-operator and so every attempt to implement something like Monads just end up using (basically untyped) subs and so you loose all the benefits of compile-time type-checking.

    Or what do you think?

Re: Functional programming ?
by repellent (Priest) on Apr 17, 2009 at 21:20 UTC
    How timely. I have just uploaded my very first module IPC::Exe to CPAN.

    It uses techniques from functional programming to do IPC, and bears a striking resemblance to monads. You may study the source at your own peril.

    Thanks++ for the links. While I was writing my module, I didn't fully understand what monads were.
      IPC::Exe has an interesting (in positive sense) syntax. I'll definitely check it out further.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://758107]
Approved by lidden
Front-paged by Arunbear
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2017-12-16 10:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What programming language do you hate the most?




















    Results (449 votes). Check out past polls.

    Notices?