Tutorial by Examples

Maybe Maybe is a Functor containing a possibly-absent value: instance Functor Maybe where fmap f Nothing = Nothing fmap f (Just x) = Just (f x) Maybe's instance of Functor applies a function to a value wrapped in a Just. If the computation has previously failed (so the Maybe value is ...
class Functor f where fmap :: (a -> b) -> f a -> f b One way of looking at it is that fmap lifts a function of values into a function of values in a context f. A correct instance of Functor should satisfy the functor laws, though these are not enforced by the compiler: fmap id = i...
The Data.Functor module contains two combinators, <$ and $>, which ignore all of the values contained in a functor, replacing them all with a single constant value. infixl 4 <$, $> <$ :: Functor f => a -> f b -> f a (<$) = fmap . const $> :: Functor f => f a ...
There's a useful set of type combinators for building big Functors out of smaller ones. These are instructive as example instances of Functor, and they're also useful as a technique for generic programming, because they can be used to represent a large class of common functors. The identity functor...
A Functor is defined in category theory as a structure-preserving map (a 'homomorphism') between categories. Specifically, (all) objects are mapped to objects, and (all) arrows are mapped to arrows, such that the category laws are preserved. The category in which objects are Haskell types and morph...
The DeriveFunctor language extension allows GHC to generate instances of Functor automatically. {-# LANGUAGE DeriveFunctor #-} data List a = Nil | Cons a (List a) deriving Functor -- instance Functor List where -- automatically defined -- fmap f Nil = Nil -- fmap f (Cons x xs...

Page 1 of 1