Comment

Comment by chongli

groups as an example

Why not go all the way and teach functors and applicatives before monads? Then the student can see that monads are just a small addition built on top of the other two. Functors, in particular, are very easy to grasp despite their intimidating name. They just generalize map over arbitrary data structures:

    l_double : List Int -> List Int
    l_double xs = map (* 2) xs

    f_double : Functor f => f Int -> f Int
    f_double xs = map (* 2) xs
Applicatives are a little bit trickier but once you get them, there's only a tiny jump to get to monads. Taught this way, people will realize that they don't need the full power of monads for everything. Then, when people learn about idiom brackets [1], they start to get really excited! Instead of writing this:

    m_add : Maybe Int -> Maybe Int -> Maybe Int
    m_add x y = case x of
                     Nothing => Nothing
                     Just x' => case y of
                                     Nothing => Nothing
                                     Just y' => Just (x' + y')
You can write this:

    m_add' : Maybe Int -> Maybe Int -> Maybe Int
    m_add' x y = [| x + y |]
Much better!

[1] http://docs.idris-lang.org/en/latest/tutorial/interfaces.htm...

parent chongli 7 years ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton