
Are there cases (function or list) where the result of foldl (or foldr)would be different that foldl' (or foldr')? thanks, daryoush

"Daryoush Mehrtash"
Are there cases (function or list) where the result of foldl (or foldr)would be different that foldl' (or foldr')?
Yes, in two cases folds can differ: a) You've got an infinite list, in which case you don't want to go to the end of it before starting to return values b) You don't have infinite memory and thus need things to be strict (or just don't want to pay the overhead of allocating millions of thunks for no good reason whatsoever, in case you're lucky enough to have enough memory for your data) -- (c) this sig last receiving data processing entity. Inspect headers for copyright history. All rights reserved. Copying, hiring, renting, performance and/or quoting of this signature prohibited.

On Tue, 2008-11-04 at 15:08 -0800, Daryoush Mehrtash wrote:
Are there cases (function or list) where the result of foldl (or foldr)would be different that foldl' (or foldr')?
There is no foldr'. And yes, foldl and foldl' are different functions and thus return different results on some inputs; however, you almost always want foldl' (v. foldl). See http://www.haskell.org/haskellwiki/Stack_overflow

Am Mittwoch, 5. November 2008 00:08 schrieb Daryoush Mehrtash:
Are there cases (function or list) where the result of foldl (or foldr)would be different that foldl' (or foldr')?
thanks,
daryoush
Simple example: import Data.List weird :: Int -> Int -> Int weird _ 0 = 0 weird x y = x*y list :: [Int] list = [1, 2, 3, 4, undefined, 6, 7, 8, 9, 0] okey = foldl weird 1 list boom = foldl' weird 1 list *Main> okey 0 *Main> boom *** Exception: Prelude.undefined since foldl' evaluates strictly (to WHNF), it can die on encountering an undefined value in the list where foldl doesn't.

Excerpts from daniel.is.fischer's message of Wed Nov 05 00:37:47 +0100 2008:
Am Mittwoch, 5. November 2008 00:08 schrieb Daryoush Mehrtash:
Are there cases (function or list) where the result of foldl (or foldr)would be different that foldl' (or foldr')?
thanks,
daryoush
Simple example: import Data.List
weird :: Int -> Int -> Int weird _ 0 = 0 weird x y = x*y
list :: [Int] list = [1, 2, 3, 4, undefined, 6, 7, 8, 9, 0]
okey = foldl weird 1 list
boom = foldl' weird 1 list
*Main> okey 0 *Main> boom *** Exception: Prelude.undefined
since foldl' evaluates strictly (to WHNF), it can die on encountering an undefined value in the list where foldl doesn't.
Your example is a nice example of foldl over foldl', it would be nice to have it in the wiki page about the different folds[1]. Best regards, [1]: http://haskell.org/haskellwiki/Foldr_Foldl_Foldl%27 -- Nicolas Pouillard aka Ertai

Lets assume we don't have undefined in the list, are there functions (or
properties in the function) that would cause foldl to have different results
than foldl'?
daryoush
On Tue, Nov 4, 2008 at 3:37 PM, Daniel Fischer
Am Mittwoch, 5. November 2008 00:08 schrieb Daryoush Mehrtash:
Are there cases (function or list) where the result of foldl (or foldr)would be different that foldl' (or foldr')?
thanks,
daryoush
Simple example: import Data.List
weird :: Int -> Int -> Int weird _ 0 = 0 weird x y = x*y
list :: [Int] list = [1, 2, 3, 4, undefined, 6, 7, 8, 9, 0]
okey = foldl weird 1 list
boom = foldl' weird 1 list
*Main> okey 0 *Main> boom *** Exception: Prelude.undefined
since foldl' evaluates strictly (to WHNF), it can die on encountering an undefined value in the list where foldl doesn't.

On Wed, 2008-11-05 at 10:01 -0800, Daryoush Mehrtash wrote:
Lets assume we don't have undefined in the list, are there functions (or properties in the function) that would cause foldl to have different results than foldl'?
If the function is partial on some elements of the list. (3 /), for example, if the list contains 0. If f is total over the elements of the list (whether the elements of the list are partial or total) and f z /= _|_, then foldl' f z = foldl f z. jcc

On Wed, 2008-11-05 at 10:01 -0800, Daryoush Mehrtash wrote:
Lets assume we don't have undefined in the list, are there functions (or properties in the function) that would cause foldl to have different results than foldl'?
The only difference in the definition of foldl and foldl' is a seq so it can only differ due to bottoms as far as semantics is concerned.

Derek Elkins
On Wed, 2008-11-05 at 10:01 -0800, Daryoush Mehrtash wrote:
Lets assume we don't have undefined in the list, are there functions (or properties in the function) that would cause foldl to have different results than foldl'?
The only difference in the definition of foldl and foldl' is a seq so it can only differ due to bottoms as far as semantics is concerned.
Denotational semantics that is, isn't it? -- (c) this sig last receiving data processing entity. Inspect headers for copyright history. All rights reserved. Copying, hiring, renting, performance and/or quoting of this signature prohibited.

2008/11/5 Daryoush Mehrtash
Are there cases (function or list) where the result of foldl (or foldr)would be different that foldl' (or foldr')?
Maybe this wiki article I wrote some time ago will answer your question: http://haskell.org/haskellwiki/Foldr_Foldl_Foldl' regards, Bas

On Wed, Nov 5, 2008 at 12:43 AM, Bas van Dijk
2008/11/5 Daryoush Mehrtash
: Are there cases (function or list) where the result of foldl (or foldr)would be different that foldl' (or foldr')?
Maybe this wiki article I wrote some time ago will answer your question:
Oops that link should be: http://haskell.org/haskellwiki/Foldr_Foldl_Foldl%27

Bas van Dijk wrote:
On Wed, Nov 5, 2008 at 12:43 AM, Bas van Dijk
wrote: 2008/11/5 Daryoush Mehrtash
: Are there cases (function or list) where the result of foldl (or foldr)would be different that foldl' (or foldr')? Maybe this wiki article I wrote some time ago will answer your question:
Oops that link should be:
I have an idea for the foldl diagram. If you rotate the RHS 90deg clockwise, it slopes the same way as the original list, but the 'f's are still rotated sideways to signify which their inputs are. That makes the relationship between imperative accumulator loops and foldl a bit clearer to me, as well as the relationship between the list and the recursion structure. -- src/
participants (8)
-
Achim Schneider
-
Bas van Dijk
-
Daniel Fischer
-
Daryoush Mehrtash
-
Derek Elkins
-
Jonathan Cast
-
Nicolas Pouillard
-
Simon Richard Clarkstone