[Haskell-beginners] applicative instance
sasa bogicevic
brutallesale at gmail.com
Sat Jan 28 09:53:07 UTC 2017
Yep that worked, thanks.
Is there a way to do it without defining a separate function like plusPlus ?
> On Jan 28, 2017, at 10:43, Francesco Ariis <fa-ml at ariis.it> wrote:
>
> 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)
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
More information about the Beginners
mailing list