Re: [Haskell-cafe] foldl vs foldl'

2008-11-09 Thread Simon Richard Clarkstone

Bas van Dijk wrote:

On Wed, Nov 5, 2008 at 12:43 AM, Bas van Dijk [EMAIL PROTECTED] wrote:

2008/11/5 Daryoush Mehrtash [EMAIL PROTECTED]:

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'


Oops that link should be:

http://haskell.org/haskellwiki/Foldr_Foldl_Foldl%27


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/
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] foldl vs foldl'

2008-11-05 Thread Nicolas Pouillard
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
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] foldl vs foldl'

2008-11-05 Thread Daryoush Mehrtash
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 [EMAIL PROTECTED]wrote:

 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.

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] foldl vs foldl'

2008-11-05 Thread Jonathan Cast
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


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] foldl vs foldl'

2008-11-05 Thread 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.

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] foldl vs foldl'

2008-11-04 Thread Derek Elkins
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

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] foldl vs foldl'

2008-11-04 Thread 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.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] foldl vs foldl'

2008-11-04 Thread Bas van Dijk
2008/11/5 Daryoush Mehrtash [EMAIL PROTECTED]:
 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
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] foldl vs foldl'

2008-11-04 Thread Bas van Dijk
On Wed, Nov 5, 2008 at 12:43 AM, Bas van Dijk [EMAIL PROTECTED] wrote:
 2008/11/5 Daryoush Mehrtash [EMAIL PROTECTED]:
 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'

Oops that link should be:

http://haskell.org/haskellwiki/Foldr_Foldl_Foldl%27
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe