|go ahead... be a heretic|
Is it worth using Monads in Perl ? and what the Monads are ?by rootcho (Pilgrim)
|on Jun 10, 2007 at 23:43 UTC||Need Help??|
rootcho has asked for the wisdom of the Perl Monks concerning the following question:
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 :
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 :
- 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)
Then in our main program we do something like this :
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). :
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.