6 ms·
My general problem with Haskell articles and such: they are written as if you already understand Haskell. They make total sense if you know the language, but if
by jnordwick 10y ago
My general problem with Haskell articles and such: they are written as if you already understand Haskell. They make total sense if you know the language, but if you are trying to learn are mostly useless if not more confusing. Or even worse, they devolve into dense academic prose even when writing introductory articles.
Sometimes they even use Haskell as if you already know it to try to explain it.
I'm still looking for a basic article that describes monads well. My first few languages were all functional too, so that isn't the problem. I even still use APL derivatives.
- seagreen 10y agohttp://dev.stephendiehl.com/hask/#monads http://dev.stephendiehl.com/hask/#monads
- jnordwick 10y agoAre you serious? > Monads are not complicated. They are implemented as a typeclass with two methods, return and (>>=) (pronounced "bind"). In order to implement a Monad instance, these two functions must be defined in accordance with the arity described in the typeclass definition: This is a great example of what I just said: it uses complex Haskell code and functions to describe a monad. If you already know what it assumes you know, you most likely already know what a monad is.
- setra 10y agoIf I define this in c++ lingo words barely change. You take a c++ parent class with two virtual methods. These methods are "return" and "bind". In order to implement a monad instance you must derive from this parents class and implement the virtual methods.
- jnordwick 10y agoWhat is bind and what is return? What do they do?
- setra 10y agoI was translating your quote to c++ lingo not defining functions. return takes a value and returns an instance of your template class containing the value. bind takes an instance of your (template class A) a function of (type a ) to (template class B) and produces a value of (template class B). If you don't care about the "laws" your definitions are expected to follow thats it. The idea bind is you take a value of a template class such as (std::vector<Int> type) the value {3}, then based on value either pass it to the next function or return it. one definition of bind would be if the vector is empty return it, if its not empty pass it to this function and return the result of that function. return is just a way of placing a value into your template container class.
- deleted 10y ago[deleted]
- seagreen 10y agoI posted that because I think it's the best you're going to get. Monads can be two things: (1) a concept in category theory, (2) a rough implementation of that concept in a programming language. If you want to learn about monads from the category theory perspective, great! This is really hard though, I wouldn't recommend it for most people (and haven't really even succeeded myself). If you want to learn about a specific programming language's implementation of monads, you will naturally have to know how that language works. This road is far easier than category theory, but still requires knowing (for Haskell) what a typeclass is, what a higher-kinded type is, etc. It sounds like you want an explanation without prereqs from either category theory or Haskell. If you google for monad tutorials you will find dozens of quick explanations, but I personally don't think this is a good way to actually understand them.
- jnordwick 10y agoDescribe monads to Lisp, Scheme, APL person. You can do that with the y-combinator but why not monads?
- tel 10y agoBecause these languages lack a way of discussing something at the right "higher order" as Monads. You can implement them, but there's no word for the kind of thing a monad is. There's not even a real word for a type. You need types. Then you need "higher order types" or parameterized types. Then you need a way to discuss a common set of operations which appear repeatedly for some of these parameterized types and it can't be OO-like. You can exemplify the operations in, e.g., Lisp, but you have a hard time "talking about" the thing. --- In Scheme, monads are the answer to the following question: The functions (define (pure a) (list a)) and (define (join ls) (foldl append (list) ls)) are related to lists in the same way that (define (pure a) (lambda (k) (k a))) and (define (join z) (lambda (k) ((z k) k))) is related to CPS-transformed programs. What is the commonality?
- deleted 10y ago[deleted]
- 10y ago
- Nadya 10y agoTo understand monads one must first understand monads. I'm not even saying that a bit sarcastically. I've read countless "tutorials" and even tried messing around with Haskell in an attempt to grok monads. According to the link shared by GP comment, my understanding is still completely wrong. There is a reason there is a joke about it. "A monad is just a monoid in the category of endofunctors, what's the probleⅿ?" - James Iry, taken from [0] [0] http://james-iry.blogspot.com/2009/05/brief-incomplete-and-mostly-wrong.html http://james-iry.blogspot.com/2009/05/brief-incomplete-and-m...
- Smaug123 10y agoI consider that quote to be a demonstration of the simple/easy axes referenced in a relative of this comment. It demonstrates that monads are simple things, since endofunctors, categories and monoids are all simple concepts.
- jghn 10y agoAs a scala developer the thing which got me on my way was the explanation "something which has a flatMap method". That's not entirely true but it gets you in the right ballpark.
- deleted 10y ago[deleted]
- zodiac 10y agoWell, you really should know the amount of Haskell it assumes you know (typeclasses, maybe infix functions, arity etc) before learning about Haskell monads. Otherwise you're learning multiple concepts at once - why not learn about those other concepts in other contexts first? The Stephen Diel article even lists the prerequisites explicitly. My favourite monad explanation is Tikhon Jelvis's at https://www.quora.com/What-are-monads-in-functional-programming-and-why-are-they-useful https://www.quora.com/What-are-monads-in-functional-programm..., but reading through it, it does assume some prerequisite knowledge about Haskell.
- chongli 10y agoSimple /= easy. Complex /= hard. Monads are very simple. They just aren't easy because most people don't have the base of terminology. When you grok them you'll laugh because you'll realize how simple and powerful and obvious they seem in hindsight. This is the reason so many people write monad tutorials after they learn them. It's actually really weird. Does anyone else have some examples of things they learned which seemed really difficult but turned out to be really simple in hindsight?
- smallnamespace 10y ago> They just aren't easy because most people don't have the base of terminology That's like saying a giant, complex library with a nice, tight API is 'simple'. It's not, the complexity is just hidden. Same thing with monads. Once you understand some of the machinery of category theory, or really grok functional programming, then yes, monads are 'simple', but all the complexity lies in getting there.
- kccqzy 10y agoI think you're confusing complexity with learning curve. Complex things can be made easy to learn, if, for example, you have "a nice, tight API." Monads are the opposite. They are simple things that are difficult to learn.
- smallnamespace 10y agoWhy would something be difficult to learn if there wasn't complexity underneath it? There are lots of things that are simple to state, like the fundamental theorem of algebra, but the machinery behind it is complex.
- chousuke 10y agoA simple thing can be difficult because understanding why it works may be nontrivial. However, it remains simple (by definition) if understanding is not affected by complecting factors such as side-effects or the global state of the system. A pure function is always simple, even if it implements a difficult algorithm. I hold the opinion that complicated things can never be truly easy: they can only have the appearance of ease until there comes a time that you must understand all the complexity.
- platz 10y agoJust relax a little bit, the OP post is essentially notes from someone learning the language, not a "greybeard"
- ycmbntrthrwaway 10y agoRead this: http://learnyouahaskell.com/chapters http://learnyouahaskell.com/chapters Just don't skip chapters and read from the beginning to the end.
- alok-g 10y agoYou advice here may be good; am not questioning that. However, question does come why can't there be a shorter explanation for people who already know programming. Most people are unable to explain it well, or at all, leading me towards a belief that they don't get it themselves as yet.
- ycmbntrthrwaway 10y agoYou can explain what a monad, for example, is, in one sentence, but it is useless if you don't show how it is used. Similarly, you may know what eigenvector, or derivative function, is, but without practice you can't understand why you may want it.
- alok-g 10y agoI'm not looking for a single sentence explanation or definition. I'm willing to read more, and the structure you suggest of showing precisely worded definitions along with positive and negative examples would be great! However, I would not want to start with reading how to install a compiler, how to add two numbers in Haskell, etc. I have similar issues elsewhere when people start with hello worlds and getting started in introductions to projects and programming languages without spending enough time on what the thing is about.
- pyrale 10y ago> why can't there be a shorter explanation for people who already know programming. Why can't you learn chinese very fast if you already know a handful of european languages ? The answer is the same : the underlying concepts are different. Sadly, there is no easy shortcut to understand these concepts. The best that can be done (and it's hard enough already), is to make learning entertaining.
- z1mm32m4n 10y agoThis was the article that finally made monads "click" for me when learning Haskell: https://slpopejoy.github.io/posts/Effectful01.html https://slpopejoy.github.io/posts/Effectful01.html
- BoiledCabbage 10y agoI've noticed the same thing - Haskellers are terrible at describing them. I'm not at all a Haskell expert, but I think I understand enough of Monads to explain them. Yes they are as simple as people claim they are, and if you've done any functional programming you may already know them. 1. Imagine a datastructure and a value. In this case we'll use "List" as the datastructure, and "Int" as the value. So [3,4,5]. It's type is List<Int>. 2. Next we'll take "f" as a function that converts a single value to a list of values (ie a datastructure of our values). In this case the function takes a value and outputs a list of 3 copies of the value. ex. f(8) = [8,8,8] The type of f is: Int -> List<Int> // Takes in an int, and returns a list of ints. Also imagine a function "g" that does the same thing but makes 5 copies of the data. ex. g(7) = [7,7,7,7,7] It has the same type as f: Int -> List<Int> 3. How we apply the function to the data: Frequently when we have lists and functions over their values we will call map over them. _.map(f, [3,4,5]) = [[3,3,3],[4,4,4],[5,5,5]] Notice that we don't get out a list of ints (type: List<Int>), instead we have a list of lists of Ints (type: List<List<Int>>). We also often like to create/compose chains of these functions to create a pipeline to transform the data. So normally we would want to do something like: r1 = _.map(f, [3,4,5]) r2 = _.map(g, r1) But we can't. r1 is of the wrong type to pass into the second map. r1 is a List<List<Int>>, but the g is expecting a List<Int>. So how do we fix this. How do we turn [[3,3,3],[4,4,4],[5,5,5]] into a flattened list of ints? We append them.: [3,3,3] ++ [4,4,4] ++ [5,5,5] // where ++ is list append in this language the end result is [3,3,3,4,4,4,5,5,5]. Which is perfect. This is now something that we can pass into the _.map(g, ...) above. We first 'mapped' transforming List<Int> -> List<List<Int>>. Then we "flattened" so that List<List<Int>> became just List<Int>. This allowed us to run map with g next. This combination is bind. Now the absurd part. If you've used something like C#'s linq there is a function that does this. It's called "SelectMany()". SelectMany is bind. It converts each element in a list to a new list, then flattens and appends the nested lists into a single list. Bind is nothing more than SelectMany(). The only notable piece is that bind is abstract. There are different incarnations of bind for each datastructure. SelectMany is the bind for Lists. There is a different implementation of bind for "Maybe" type. Or other datastructures. And a Monad is nothing more than a datastructure that supports SelectMany/bind. List is the one that you use all the time. The selectMany concept generalized to any datastructure is the concept of bind + a Monad.
- moomin 10y agoActually, monads are pretty easy to understand, but learning to use them is harder. Imagine two functions : A -> k B B -> k C Can you turn those into A -> k C Such that you can just think of it as functional composition? If you can, k is a monad. The catch is, knowing what a Monad is is a bit like knowing what a saxophone is... the real problem is learning how to play it.
- edem 10y agoCool. What does `k` stand for? A function?
- moomin 10y agoA higher-level type (which is a function, if you allow functions in your type system). Examples: List of a One or zero of a A randomly chosen a A function that takes an s and returns an a and another s i.e. s -> (s, a) aka "a stateful computation returning a) A routine that reads a file and returns an a. In each case, the point is that you can a) write code that acts as if the k isn't there and hence b) re-use code between all of these things without changing it. I'm going to take the opportunity to recommend http://www.cis.upenn.edu/~cis194/spring13/ http://www.cis.upenn.edu/~cis194/spring13/. Do all of the exercises, even the really basic ones. What you learn by the end will blow your mind.
- edem 10y agoThanks, I'll try it out! Is it applicable for someone with zero Haskell experience? Am I supposed to join a class which starts somewhen or how this site works?
- moomin 10y agoIt's literally just Brent Yorgey's lecture notes. Read the notes for week 1, do the exercises, repeat. I learned Haskell from scratch using it. Use stack and a random text editor. What it won't do is make you fluent in modern Haskell. That would be a much longer course. However, I guarantee that if you work through it you will have some great "Wow" moments.
- JoelSanchez 10y ago> I'm still looking for a basic article that describes monads well. I really like this one: http://adit.io/posts/2013-04-17-functors,_applicatives,_and_monads_in_pictures.html http://adit.io/posts/2013-04-17-functors,_applicatives,_and_...
- edem 10y agoTry this one. Haskell in pictures: http://adit.io/posts/2013-04-17-functors,_applicatives,_and_monads_in_pictures.html http://adit.io/posts/2013-04-17-functors,_applicatives,_and_...