<p dir="ltr">The foldr function does not start from the right. It associates to the right. </p>
<p dir="ltr">This talk specifically addresses this common mistake. </p>
<p dir="ltr"><a href="http://functionaltalks.org/2013/06/19/tony-morris-explain-list-folds-to-yourself/">http://functionaltalks.org/2013/06/19/tony-morris-explain-list-folds-to-yourself/</a></p>
<div class="gmail_quote">On 06/12/2015 10:21 AM, "Raja" <<a href="mailto:rajasharan@gmail.com">rajasharan@gmail.com</a>> wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">foldr is supposed to start folding from the right side (as the name suggests).<div>and this is why it is synonymous to "list construction" as I'm told</div><div><br></div><div>for e.g:</div><div>> foldr (:) [ ] [1,2,3,4,5]</div><div>[1,2,3,4,5]</div><div><br></div><div>In the same spirit I'm trying to construct a Foldable instance of my own type:</div><div><br></div><div>data Li a = Nil | Cons a (Li a)</div><div>    deriving (Show)</div><div><br></div><div>instance Foldable Li where</div><div>    foldr f b Nil = b</div><div>    foldr f b (Cons a y) = foldr f (f a b) y</div><div><br></div><div>So I'm trying out foldr for my type:</div><div>> foldr Cons Nil (Cons 1 (Cons 2 Nil))</div><div>Cons 2 (Cons 1 Nil)</div><div><br></div><div>This shows my foldr implementation i'm not folding from right side, </div><div>but how can I possibly do that - the data could have been an infinite stream.</div><div>It feels like I will never be able to truly write a foldr implementation with "right" folding mechanism.</div><div><br></div><div>Any thoughts?</div><div><br></div></div>
<br>_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br>
<br></blockquote></div>