<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<div class="moz-cite-prefix"><br>
<tt>On 28-Jan-2017 4:53 AM, sasa bogicevic wrote:<br>
</tt>
<blockquote
cite="mid:432542C4-01E7-4E22-A563-7068906C4335@gmail.com"
type="cite">Is there a way to do it without defining a separate
function like plusPlus ?</blockquote>
<tt><br>
My guess is there isn't. I'm unsure what you mean by your
question though.</tt><br>
<tt><br>
</tt><tt>Your List is a reimplementation of Haskell []. So, List
with the "Cartesian product" Applicative instance will, like
Haskell [], extend to a Monad instance. In the Monad instance
for [], join = concat, and the work of concat is done using ++.</tt><tt><br>
</tt><tt><br>
</tt><tt>For List, we can implement join using concatList:</tt><tt><br>
</tt><tt><br>
</tt><tt>concatList :: List (List a) -> List a</tt><tt><br>
</tt><tt>concatList Nil = Nil</tt><tt><br>
</tt><tt>concatList (Cons xs xss) = xs `plusPlus` (concatList xss)</tt><tt><br>
</tt><tt><br>
</tt><tt>and then we can add a Monad instance for List:</tt><tt><br>
</tt><tt><br>
</tt><tt>instance Monad List where</tt><tt><br>
</tt><tt> return = pure</tt><tt><br>
</tt><tt> xs >>= f = concatList (pure f <*> xs)</tt><tt><br>
</tt><tt><br>
</tt><tt>or equivalently, bind is</tt><tt> given by<br>
</tt><tt> xs >>= f = concatList (fmap f xs)<br>
</tt><tt><br>
To ask whether you can define the Cartesian product Applicative
instance for List without plusPlus, is, I think, like asking
whether there is a Cartesian product Applicative instance for
List (or []) which doesn't extend to a Monad instance. Because
if it does extend to a Monad (that obeys the Monad laws), then
there will exist an implementation of join :: List (List a)
-> List a, and join will need to collapse a List of Lists
into a List. A function like plusPlus is used to accomplish the
collapse.<br>
<br>
That's "proof by hand waving."<br>
<br>
The Ziplist Applicative instance for List on the other hand
can't be extended to a Monad instance without additional
restrictions on the lengths of the lists. Your question led me
to some interesting reading with a google search on "list monad
vs ziplist". Thanks.<br>
<br>
<br>
</tt>On 28-Jan-2017 4:53 AM, sasa bogicevic wrote:<br>
</div>
<blockquote
cite="mid:432542C4-01E7-4E22-A563-7068906C4335@gmail.com"
type="cite">
<pre wrap="">Yep that worked, thanks.
Is there a way to do it without defining a separate function like plusPlus ?
</pre>
<blockquote type="cite">
<pre wrap="">On Jan 28, 2017, at 10:43, Francesco Ariis <a class="moz-txt-link-rfc2396E" href="mailto:fa-ml@ariis.it"><fa-ml@ariis.it></a> wrote:
On Sat, Jan 28, 2017 at 10:09:10AM +0100, sasa bogicevic wrote:
</pre>
<blockquote type="cite">
<pre wrap="">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.
</pre>
</blockquote>
<pre wrap="">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
<a class="moz-txt-link-abbreviated" href="mailto:Beginners@haskell.org">Beginners@haskell.org</a>
<a class="moz-txt-link-freetext" href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a>
</pre>
</blockquote>
<pre wrap="">_______________________________________________
Beginners mailing list
<a class="moz-txt-link-abbreviated" href="mailto:Beginners@haskell.org">Beginners@haskell.org</a>
<a class="moz-txt-link-freetext" href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a>
</pre>
</blockquote>
<p><br>
</p>
</body>
</html>