Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re: Functional programming ?

by BrowserUk (Patriarch)
on Apr 17, 2009 at 03:48 UTC ( #758119=note: print w/replies, xml ) Need Help??

in reply to Functional programming ?

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.

Replies are listed 'Best First'.
Re^2: Functional programming ?
by tilly (Archbishop) on Apr 17, 2009 at 05:17 UTC
    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.

Re^2: Functional programming ?
by CountZero (Bishop) on Apr 17, 2009 at 06:11 UTC
    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 ...?


    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

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2022-05-20 09:38 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (73 votes). Check out past polls.