Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. Re: applicative instance (Francesco Ariis)
2. Re: applicative instance (sasa bogicevic)
3. Re: applicative instance (Francesco Ariis)
4. Re: applicative instance (sasa bogicevic)
----------------------------------------------------------------------
Message: 1
Date: Fri, 27 Jan 2017 23:21:35 +0100
From: Francesco Ariis <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] applicative instance
Message-ID: <[email protected]>
Content-Type: text/plain; charset=utf-8
On Fri, Jan 27, 2017 at 11:09:07PM +0100, sasa bogicevic wrote:
> What is wrong with my applicative instance for custom List type ?
>
> http://lpaste.net/351723
>
> [..]
You have implemented <*> on a list in one of the many possible
(and sensible) ways. In regular Haskell it's a ZipList
λ> :m Control.Applicative
λ> ZipList [(+1), (*2)] <*> ZipList [1,2]
ZipList {getZipList = [2,4]}
(and there's nothing wrong with that)
------------------------------
Message: 2
Date: Sat, 28 Jan 2017 10:09:10 +0100
From: sasa bogicevic <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] applicative instance
Message-ID: <[email protected]>
Content-Type: text/plain; charset=utf-8
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.
Thanks, Sasa
> On Jan 27, 2017, at 23:21, Francesco Ariis <[email protected]> wrote:
>
> On Fri, Jan 27, 2017 at 11:09:07PM +0100, sasa bogicevic wrote:
>> What is wrong with my applicative instance for custom List type ?
>>
>> http://lpaste.net/351723
>>
>> [..]
>
> You have implemented <*> on a list in one of the many possible
> (and sensible) ways. In regular Haskell it's a ZipList
>
> λ> :m Control.Applicative
> λ> ZipList [(+1), (*2)] <*> ZipList [1,2]
> ZipList {getZipList = [2,4]}
>
> (and there's nothing wrong with that)
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
------------------------------
Message: 3
Date: Sat, 28 Jan 2017 10:43:45 +0100
From: Francesco Ariis <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] applicative instance
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
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)
------------------------------
Message: 4
Date: Sat, 28 Jan 2017 10:53:07 +0100
From: sasa bogicevic <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] applicative instance
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
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 <[email protected]> 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
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
------------------------------
Subject: Digest Footer
_______________________________________________
Beginners mailing list
[email protected]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
------------------------------
End of Beginners Digest, Vol 103, Issue 24
******************************************