Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
hi,

I was reading lately some Haskell tutorials. The thing that come and come again was Monads :).
There is so many tutorials on Monads over the internet but almost none of them I think is describing the Monads well.
In most of the cases they explain just part of it..what I mean first most of them are Haskell oriented (nothing bad with that but, generally most of the ppl still learn Haskell or even don't want to learn Haskell at all, so it is of no bigger use for them), other tutorials underline the mechanics of Monads these are a litte better because most ppl coming from imperative background can relate things.
And finally there is those written from the perspective of other languages Perl,Python, Ruby, JS... the worst thing about them is that they will show you how to implement Maybe and/or State monads, which is good but ..:(
What all of them forget to explain why the hell you will want to use these monads in imperative languages !?
Is there any use at all and if yes when ?

So as I'm saynig there is no one good tutorial on the net for Monads. You have to read them all to combine the pieces to figure something at all.

The best tutorial IMHO is this one :
http://en.wikibooks.org/wiki/Haskell/Understanding_monads

OK. I'm not trying to blame the authors of writing bad tutorials what I'm tring to say is that they always concentrate on a narrow view of either Monad mechanics or usage or examples or Haskell ;).

I will try to explain this to myself and to you (please be warned that I'm not totaly sure that I understand Monads myself ;) /so bear with me and don't blame me for being totally wrong/, what I'm trying is to provoke a discussion on the Monads mechanics, examples in Perl, when to use them, why to use them..etc..

Let start. From what I read the Monads in Haskell are used to bring ability to add control flow to the language (like the imperative languages). (Haskell is more like Math, rather than C-like lang where you describe the steps by step description how to solve a problem)

So if we have to translate this to Perl and similar lang. we should use Monads (design pattern :), some go nuts when they hear this) when we have to do "out-of-order" operations, which break the flow of the program. What are these things , IMO :

- Exceptions
- Debugging
- Logging
- State handling
- etc...fill in


We have one additional benefit to this and it is the ability to interfere to all this activities from outside of the normal-flow too, what I mean f.e. enabling/disabling debuging/logging or modifing the handling of them w/o changing our original program which is a good thing.

So what is the simplest way to implement Monads in Perl (for now w/o using objects). In general from my understaning it should be something like this (in its simplest possible form) : ("bind" is the focal point of the whole monad thing)
sub bind { my ($callback, @args) = @_; $callback->(@args); }
Then in our main program we do something like this :
bind(\&debug, "Entering xxx()"); ... bind(\&logit, "Username : $user logged in") ... and so on...
By itself bind() it is not that interesting. But now we can modify it to do millon other things we have not forseen when we wrote our program.
This is the point where I start loosing the grip with the monads. Normally in the examples instead of passing @args, they pack them in a container which would look more like this (if i'm not totaly wrong). :
sub bind { my $monad = shift;#self my $callback = shift; my @args = @_; my @res = $callback->(@args); $monad->pack_args(@res); return $monad } my $monad = new Monad; $monad->bind(\&debug, "Entering xxx()"); ... $monad->bind(\&logit, "Username : $user logged in") ... and so on...
I.e. our callback function will always get the arg as normal args not like monad. And then like result we will get the monad again and so on.. Still don't quite understand the purpose of all this (if i can do it simpler in the first case). Except that now I can modify the Monad-object to store much more state and info and of course that OO-way is more scalable.
(Yes they also mention they want to separate/decouple the things in a way so that Monad live in its own universe, so that your original program logic is not modified when you modify the Monad. I can see the benefit of this ) What I'm worried is that it now becomes much more hard to use, I mean alot more typing and of course it becomes slower.

And finally my last point, this seem alot to me like Aspect-design pattern ?!

So again excuse my total ignorance ;) and please explain and elaborate more on the topic.

Thanx for your attention.

In reply to Is it worth using Monads in Perl ? and what the Monads are ? by rootcho

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (3)
As of 2024-03-29 01:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found