foldr, Foldable and right-side folding

foldr is supposed to start folding from the right side (as the name suggests). and this is why it is synonymous to "list construction" as I'm told for e.g:
foldr (:) [ ] [1,2,3,4,5] [1,2,3,4,5]
In the same spirit I'm trying to construct a Foldable instance of my own type: data Li a = Nil | Cons a (Li a) deriving (Show) instance Foldable Li where foldr f b Nil = b foldr f b (Cons a y) = foldr f (f a b) y So I'm trying out foldr for my type:
foldr Cons Nil (Cons 1 (Cons 2 Nil)) Cons 2 (Cons 1 Nil)
This shows my foldr implementation i'm not folding from right side, but how can I possibly do that - the data could have been an infinite stream. It feels like I will never be able to truly write a foldr implementation with "right" folding mechanism. Any thoughts?

On 2015-12-05 at 19:20, Raja
foldr is supposed to start folding from the right side (as the name suggests). and this is why it is synonymous to "list construction" as I'm told
for e.g:
foldr (:) [ ] [1,2,3,4,5] [1,2,3,4,5]
In the same spirit I'm trying to construct a Foldable instance of my own type:
data Li a = Nil | Cons a (Li a) deriving (Show)
instance Foldable Li where foldr f b Nil = b foldr f b (Cons a y) = foldr f (f a b) y
So I'm trying out foldr for my type:
foldr Cons Nil (Cons 1 (Cons 2 Nil)) Cons 2 (Cons 1 Nil)
This shows my foldr implementation i'm not folding from right side, but how can I possibly do that - the data could have been an infinite stream.
A right fold on an infinite stream can terminate if the function f sometimes discards it's second argument. For example, takeWhile can be implemented this way. You are right that `foldr Cons Nil` or `foldr (:) []` will not terminate on an infinite list. On the bright side, you 've written a perfectly good left fold, even though it doesn't have quite the signature Haskell gives foldl. bergey

On Sat, Dec 5, 2015 at 10:24 PM, Daniel Bergey
On 2015-12-05 at 19:20, Raja
wrote: foldr is supposed to start folding from the right side (as the name suggests). and this is why it is synonymous to "list construction" as I'm told
for e.g:
foldr (:) [ ] [1,2,3,4,5] [1,2,3,4,5]
In the same spirit I'm trying to construct a Foldable instance of my own type:
data Li a = Nil | Cons a (Li a) deriving (Show)
instance Foldable Li where foldr f b Nil = b foldr f b (Cons a y) = foldr f (f a b) y
So I'm trying out foldr for my type:
foldr Cons Nil (Cons 1 (Cons 2 Nil)) Cons 2 (Cons 1 Nil)
This shows my foldr implementation i'm not folding from right side, but how can I possibly do that - the data could have been an infinite stream.
A right fold on an infinite stream can terminate if the function f sometimes discards it's second argument. For example, takeWhile can be implemented this way.
You are right that `foldr Cons Nil` or `foldr (:) []` will not terminate on an infinite list.
On the bright side, you 've written a perfectly good left fold, even though it doesn't have quite the signature Haskell gives foldl.
I see - I did write a foldl impl. instance Foldable Li where foldr f b Nil = b foldr f b (Cons a y) = f a (foldr f b y) Now the `b' is getting propagated all the way to right. Thanks.

On Sun, Dec 6, 2015 at 10:24 AM, Daniel Bergey
data Li a = Nil | Cons a (Li a)
deriving (Show)
instance Foldable Li where foldr f b Nil = b foldr f b (Cons a y) = foldr f (f a b) y
So I'm trying out foldr for my type:
foldr Cons Nil (Cons 1 (Cons 2 Nil)) Cons 2 (Cons 1 Nil)
This shows my foldr implementation i'm not folding from right side, but how can I possibly do that - the data could have been an infinite stream.
A right fold on an infinite stream can terminate if the function f sometimes discards it's second argument. For example, takeWhile can be implemented this way.
You are right that `foldr Cons Nil` or `foldr (:) []` will not terminate on an infinite list.
It's slightly misleading to say: `foldr (:) []` -- call it the foo fold -- will not terminate on an infinite list. It suggests that folds normally terminate on an infinite list whereas this one doesn't, with the implied meaning that the foo fold is "defective" in some sense. Fact is, the foo fold is perfectly cromulent. It's equivalent to the identity function on both finite and infinite lists. So the foo fold doesn't terminate on an infinite list because -- well, duh -- the infinite list doesn't terminate. Also not all folds are designed to terminate. E.g. a list map makes sense even for infinite lists. A list map can be written as a fold. Such a fold wouldn't terminate either. -- Kim-Ee

The foldr function does not start from the right. It associates to the
right.
This talk specifically addresses this common mistake.
http://functionaltalks.org/2013/06/19/tony-morris-explain-list-folds-to-your...
On 06/12/2015 10:21 AM, "Raja"
foldr is supposed to start folding from the right side (as the name suggests). and this is why it is synonymous to "list construction" as I'm told
for e.g:
foldr (:) [ ] [1,2,3,4,5] [1,2,3,4,5]
In the same spirit I'm trying to construct a Foldable instance of my own type:
data Li a = Nil | Cons a (Li a) deriving (Show)
instance Foldable Li where foldr f b Nil = b foldr f b (Cons a y) = foldr f (f a b) y
So I'm trying out foldr for my type:
foldr Cons Nil (Cons 1 (Cons 2 Nil)) Cons 2 (Cons 1 Nil)
This shows my foldr implementation i'm not folding from right side, but how can I possibly do that - the data could have been an infinite stream. It feels like I will never be able to truly write a foldr implementation with "right" folding mechanism.
Any thoughts?
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
participants (4)
-
Daniel Bergey
-
Kim-Ee Yeoh
-
Raja
-
Tony Morris