DEV Community

chenge
chenge

Posted on

4 1

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

Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

They are all typeclasses.

ref


Functor、Applicative 和 Monad(Chinese)

Learn You a Haskell for Great Good!

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (1)

Collapse
 
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

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay