|
|
|
|
|
by chongli
2509 days ago
|
|
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... |
|
The Haskell formulation of applicatives doesn't make much sense outside of languages where currying is idiomatic---which is most languages. In these languages you tend to see the product / semigroupal formulation, and here applicatives become a bit trickier to explain as you need more machinery.