Live data from Hacker News

The Monad Challenges: Jump start your understanding of monads

mightybyte.github.io

41–50 of 51 posts

Re: The Monad Challenges: Jump start your understanding of monads

#41
post #28

I have Haskell experience and would love to do these exercises, but I am struggling with the environment. For example, Set1 tells me to make a function fiveRands and then "check my answers;" how do I do that? I know enough Haskell to do something like: main = putStrLn $ show fiveRands but putStrLn is not part of MCPrelude so this doesn't compile, and the instructions say explicitly "Do not import any other modules."…

Ignore the instructions and grab putStrLn if you need it in order to check your work. The instructions about not using other modules are there to avoid you "cheating". Looking at your work is definitely not cheating and will not prevent you from learning :). Also, just typing "fiveRands" and hitting ENTER at the repl will show you the value of fiveRands (or any other expression).

"or any other expression"

To be more precise, any other expression with an appropriate type. That's `a` or `IO a`, where `a` is an instance of `Show`.

Most things that can reasonably be Shown already have a `Show` instance, but some things (like functions) can't really.

This is relevant when the REPL shows an error like:

    :2:1:
        No instance for (Show (t0 -> a0))
          (maybe you haven't applied enough arguments to a function?)
          arising from a use of ‘print’
        In the first argument of ‘print’, namely ‘it’
        In a stmt of an interactive GHCi command: print it

Re: The Monad Challenges: Jump start your understanding of monads

#42
post #7

I found a math background is a disadvantage for learning Haskell, since most mathematicians don't really use a lot of category theory. Lately that is changing, http://math.stackexchange.com/questions/1617592/is-set-prime... There's a lot of confusion on how the math version of "functor" or "monad" maps to the CS definitions. I found this tutorial on Monads and Applicatives helpful http://adit.io/posts/2013-04-17-func…

Something that doesn't help the Math->CS understanding of Functors is, for example, that languages like C++ use "Functor" to mean other things (function pointer in this case).

Functors also mean something different in OCaml than they do in Haskell. Two different applications of the same abstract concept.

Re: The Monad Challenges: Jump start your understanding of monads

#43

I have Haskell experience and would love to do these exercises, but I am struggling with the environment. For example, Set1 tells me to make a function fiveRands and then "check my answers;" how do I do that? I know enough Haskell to do something like: main = putStrLn $ show fiveRands but putStrLn is not part of MCPrelude so this doesn't compile, and the instructions say explicitly "Do not import any other modules."…

Hi, I was assuming that people would do their playing around and answer checking in ghci. MCPrelude does not export putStrLn because it is an IO function, which is a monad and I specifically wanted to hide all the prelude's monad stuff. I suggest loading your file in ghci with "ghci Set1.hs". Then you can just type "fiveRands" at the ghci prompt and it will show you the values.

Thanks! It would be great to walk through this, in the same way that it shows the shell commands for installation under "Getting Started."

I hardly ever use the repl because I prefer to keep my tests around. I'm sheepish to admit this, but I only just now learned that you can pass a file to ghci.

Re: The Monad Challenges: Jump start your understanding of monads

#44
post #36
post #29

Earlier quoted context omitted.

It's true that you don't need to know Haskell in order to learn about monads, but Haskell is a perfectly reasonable language in which to write a monad introduction. Haskell is good for learning about monads for the same reasons monads tend to be heavily used in Haskell, but not in other languages. Monads are both part of Haskell's standard library, and have very useful special syntax (do-notation). Writing out monadi…

>Writing out monadic code explicitly gets to be a huge pain when using monads in another language. Couldn't any other language have a syntax like thing.do(\x -> other thing(x)).do( \x -> yet another thing(x)).do( a function).do( more stuff).do( etc) And then replace "\x -> whatever" with the local language's lambda syntax. The only time you get the painful nested parens is if you want thing.do( \x -> f(x).do( \y -> g…

Like JavaScript Promises?

Re: The Monad Challenges: Jump start your understanding of monads

#45
post #3

Earlier quoted context omitted.

Coming at this from a mathematician's perspective, it's complete madness to learn what a monad is before learning what a natural transformation (and therefore a functor) is. I would also consider it somewhat eccentric to do monads without learning what an adjunction is, since adjunctions are basically why we're interested in monads in the first place.

Having been mildly disappointed by the simplicity of monoids, after intimidation from the complexity in maths, i expect that something like an adjunction is something already known to a commoner in any case, just that the vocabulary, particaularies and relations to the concept in question are not always obvious.

Monoids really are simple things. Monads are a bit less so, but they're still fairly simple objects (as evidenced by the fact that they have a short description, "monoids in the category of endofunctors", even if most people don't know what that means).

Adjunctions turn up all over maths, but I've been trying for a while to come up with an example which programmers (as opposed to mathematicians) would quickly understand. Broadly speaking, they represent "the leanest way to add a particular structure to something", but of course that's pretty useless for understanding them!

Re: The Monad Challenges: Jump start your understanding of monads

#46
Yeah so here comes another monads tutorial.

People always say that monads are not that hard and you don't need to understand category theory to understand monads, but since people still keep writing tutorials for them (and thus imply that we have yet to see a perfect one that clearly explains everything you need to know about monad), I sometimes wonder that if beginners should just start from category theory.

Re: The Monad Challenges: Jump start your understanding of monads

#47

Great. I was wondering if anyone is interested in writing a book or a blog titled something to the effect of "all the brutal concepts in the languages you want to learn or respect." For example, monads in Haskell, macros and continuations in Scheme...Maybe languages like C, Forth, Prolog, Smalltalk etc also have such arcane concepts that "scare" people??

I don't know about scaring people, but many programming languages have concepts that are best demonstrated with small examples that make people say "whoa". For example, it's amazing that append() in Prolog can be run "backwards" from the concatenated result to yield all the lists that can be concatenated to produce it. Or that the monadic bind operator in Haskell (>>=) can be defined in terms of join and fmap, or tha…

    ($) = id !?

I see now why it's true, but still... Whoah!! Thanks for that!

I think your suggestion would make a terrific online book.

Re: The Monad Challenges: Jump start your understanding of monads

#48

Yeah so here comes another monads tutorial. People always say that monads are not that hard and you don't need to understand category theory to understand monads, but since people still keep writing tutorials for them (and thus imply that we have yet to see a perfect one that clearly explains everything you need to know about monad), I sometimes wonder that if beginners should just start from category theory.

I know loads of category theory, but when I tried to find out how monads were used in computer programming I was disappointing to find that no one has written a "Monads in Haskell for Category Theorists"tutorial. :-(

Re: The Monad Challenges: Jump start your understanding of monads

#49
post #24

> Do I need to know Haskell? Yes. This is enough for me to reject this blogpost. No you don't need a specific language to implement any functionality that can be implemented by a turing-complete language. I watched Crockford's monad talk [1]. He used javascript. The only problem with that talk is that he spent about 10 seconds or less explaining what a monad is (and the rest of the talk on implementation issues and h…

You need to know Haskell to complete the exercises, not to understand Monads.

That may be true in principle, but I have been unable to find any resource ever that explains them well. Presumably this is because I don't know Haskell and the only good explanations are in that language.

Re: The Monad Challenges: Jump start your understanding of monads

#50
post #17

Earlier quoted context omitted.

If you're going to talk like that, stay far, far away from anyone who is trying to learn monads. I assert that most people coming to Haskell do so because "I hear Haskell and/or FP is cool, I'll try to learn it" rather than "Category theory is so cool, wouldn't it be awesome to program that way". What you said may be true, as the theoretical foundation for why monads are useful, but for someone trying to learn to pro…

That comment of mine wasn't the spiel I'd give to someone first trying to learn about the typical use of monads in a programming context, any more than I'd lecture a beginning programmer on the general theory of commutative rings while introducing integer and floating-point arithmetic (the latter not actually comprising a commutative ring...). But teaching the use of monads to unfamiliar programmers wasn't my goal in…

Fair enough. There's a place here for technical accuracy all the way down.
Post reply on HN