DEV Community


Posted on

Typeclass(Interface) is Kernel Concept of Haskell Functor, Monad These Weird

In short, typeclass is just like interface, so it's not so hard to understand.

Let's see some code about Maybe functor, functor has a special map function:

class Functor Maybe where
    fmap :: (a -> b) -> Maybe a -> Maybe b

instance Functor Maybe where
    fmap func (Just x) = Just (func x)
    fmap func Nothing  = Nothing

Functor is a typeclass.
Applicative Functor extends Functor.
Monad extends Applicative.

class Applicative m => Monad m where
    return :: a -> m a
    (>>=) :: m a -> (a -> m b) -> m b

And another one Monoid is a math concept. It's a typeclass too.

They are all typeclasses.


Functor、Applicative 和 Monad(Chinese)

Learn You a Haskell for Great Good!

Top comments (1)

jvanbruegge profile image
Jan van Brügge

Just a small side note, in the class declaration you don't specify a concrete type:

class Functor f where
    fmap :: (a -> b) -> f a -> f b