[Haskell-beginners] applicative instance

Francesco Ariis fa-ml at ariis.it
Sat Jan 28 09:43:45 UTC 2017

On Sat, Jan 28, 2017 at 10:09:10AM +0100, sasa bogicevic wrote:
> Ok so how would the implementation look to get the correct result ?
> I can't seem to write something that will compile except ZipList version.

One way is by implementing your own (++):

    data List a = Nil | Cons a (List a) deriving (Eq, Show)

    plusPlus :: List a -> List a -> List a
    plusPlus Nil         bs = bs
    plusPlus (Cons a as) bs = Cons a (as `plusPlus` bs)

    instance Functor List where
        fmap f Nil = Nil
        fmap f (Cons a b) = Cons (f a) (fmap f b)

    instance Applicative List where
        pure x = Cons x Nil
        Nil <*> _ = Nil
        _ <*> Nil = Nil
        (Cons x xy) <*> ys = (fmap x ys) `plusPlus` (xy <*> ys)

More information about the Beginners mailing list