Zadejte hledaný výraz...

Monády ve funkcionálním programování

Narazil jsem na pár článků, které vysvětlují monády v Haskellu / funkcionálním programování.
Třeba to poslouží i někomu jinému.
http://blog.sigfpe.com/2006/08/you-could-have-invented-monads-and.html
http://importantshock.wordpress.com/2009/01/18/jquery-is-a-monad/
http://news.ycombinator.com/item?id=1997341
Několik citací:
Tři podmínky monád
Monads are abstract data types that satisfy three conditions:
1) They wrap themselves around other data types
2) They have an operation, confusingly called return, that actually performs the aforementioned wrapping
3) They have an operation called bind that allows to feed the value wrapped inside the monad into another function, as long as the function returns a monad.
Monády jako polymorfismus vyšší úrovně
Monads are a specific example of higher-kinded polymorphism, which is a very powerful and useful concept that's starting to enter the programming mainstream (examples are template concepts in C++ and LINQ in C# and .NET). Parametric polymorphism (List in Java and C#, etc) means you don't have to write a 'reverse' function that works on lists of strings, and another 'reverse' function for lists of ints, etc; you can just write a generic reverse function that works on lists of any kind, because it doesn't need to know the details of the contained type, just how to iterate over a list. However, if you want to reverse an array, you still have to write a new 'reverse'. If your language supports higher-kinded polymorphism, you can write a generic reverse function that works on any kind of container, because all it needs to know is that the container has some well-behaved method of iteration.
Monády poskytují zapouzdření, podobně jako objekty
It's a design pattern for encapsulation, with similar goals to OO but different methods. Rather than telling you what a monad is, I'll just tell you the goal.
OO is designed to hide the dangerous parts of the world from you. The dangerous parts of the world (or at least some dangerous parts of the world) are encapsulated inside little black boxes with well defined interaction points. If you interact only through those little boxes, you should be safe. For instance, a logger object gives you 1 methods: writeLog (I'll ignore logging levels). The details of actually writing logs are not your business, and you (in principle) don't need to worry about them.
Monads are stricter. A monad puts you into a box and only gives you a few interaction points with the outside world. You can do whatever you want in a monad, but you are never allowed to leave the box. Inside the logger box, you are permitted only pure functions (functions with no side effects) and writeLog.
Tahle mě zaujala: Monády řetězí výrazy, jako středníky v některých jiných jazycích
It's the operator that allows to chain two statements together. It's written ";" in Pascal, and is often implicit in other languages.
When a language explicitly talks about monads, it's because it lets you overload that operator, i.e. it let's you give alternative definitions of what it means to chain statements together.
16. 12. 2010 17:50:45
https://webtrh.cz/diskuse/monady-ve-funkcionalnim-programovani#reply592288
Pro odpověď se přihlašte.
Přihlásit