<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Dec 5, 2015 at 10:24 PM, Daniel Bergey <span dir="ltr"><<a href="mailto:bergey@alum.mit.edu" target="_blank">bergey@alum.mit.edu</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><span class="">On 2015-12-05 at 19:20, Raja <<a href="mailto:rajasharan@gmail.com">rajasharan@gmail.com</a>> wrote:<br>
> foldr is supposed to start folding from the right side (as the name<br>
> suggests).<br>
> and this is why it is synonymous to "list construction" as I'm told<br>
><br>
> for e.g:<br>
>> foldr (:) [ ] [1,2,3,4,5]<br>
> [1,2,3,4,5]<br>
><br>
> In the same spirit I'm trying to construct a Foldable instance of my own<br>
> type:<br>
><br>
> data Li a = Nil | Cons a (Li a)<br>
>     deriving (Show)<br>
><br>
> instance Foldable Li where<br>
>     foldr f b Nil = b<br>
>     foldr f b (Cons a y) = foldr f (f a b) y<br>
><br>
> So I'm trying out foldr for my type:<br>
>> foldr Cons Nil (Cons 1 (Cons 2 Nil))<br>
> Cons 2 (Cons 1 Nil)<br>
><br>
> This shows my foldr implementation i'm not folding from right side,<br>
> but how can I possibly do that - the data could have been an infinite<br>
> stream.<br>
<br>
</span>A right fold on an infinite stream can terminate if the function f<br>
sometimes discards it's second argument.  For example, takeWhile can be<br>
implemented this way.<br>
<br>
You are right that `foldr Cons Nil` or `foldr (:) []` will not terminate<br>
on an infinite list.<br>
<br>
On the bright side, you 've written a perfectly good left fold, even<br>
though it doesn't have quite the signature Haskell gives foldl.</blockquote><div><br></div><div>I see - I did write a foldl impl.</div><div><span style="color:rgb(80,0,80);font-size:12.8px"><br></span></div><div><span style="color:rgb(80,0,80);font-size:12.8px">instance Foldable Li where</span><br></div><div><span style="color:rgb(80,0,80);font-size:12.8px">    foldr f b Nil = b</span><br style="color:rgb(80,0,80);font-size:12.8px"><span style="color:rgb(80,0,80);font-size:12.8px">    foldr f b (Cons a y) = f a (foldr f b y)</span><br></div><div><span style="color:rgb(80,0,80);font-size:12.8px"><br></span></div><div><font color="#500050"><span style="font-size:12.8px">Now the `b' is getting propagated all the way to right. </span></font></div><div><font color="#500050"><span style="font-size:12.8px">Thanks.</span></font></div></div></div></div>