Re: Fwd: [Haskell-cafe] curious about sum

2009-06-15 Thread Thomas ten Cate
On Sun, Jun 14, 2009 at 21:23, Jochem Berndsenjoc...@functor.nl wrote:
 Alberto G. Corona wrote:
 Once more I forgot to send my messages to the haskell cafe list. All the
 rest of the list which I惴 suscribed to, send  the mail replies to the list
 automatically, but this doesn��. Please, can this be changed?.

 This comes up every so often, but I would be against this. Your e-mail
 client should support mailing lists.

This list does not fill out the Reply-To header. Many other lists do.
I am all for adding it, if there's no specific reason not to.

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


Re: Fwd: [Haskell-cafe] curious about sum

2009-06-15 Thread Miguel Mitrofanov
Again: what if somebody wants to answer to the original author privately? It's 
easier to just use Reply for private answers and Reply all
for list answers.

Thomas ten Cate wrote on 15.06.2009 11:18:
 On Sun, Jun 14, 2009 at 21:23, Jochem Berndsenjoc...@functor.nl wrote:
 Alberto G. Corona wrote:
 Once more I forgot to send my messages to the haskell cafe list. All the
 rest of the list which I惴 suscribed to, send  the mail replies to the list
 automatically, but this doesn��. Please, can this be changed?.
 This comes up every so often, but I would be against this. Your e-mail
 client should support mailing lists.
 
 This list does not fill out the Reply-To header. Many other lists do.
 I am all for adding it, if there's no specific reason not to.
 
 Thomas
 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe
 
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Fwd: [Haskell-cafe] curious about sum

2009-06-14 Thread Alberto G. Corona
Once more I forgot to send my messages to the haskell cafe list. All the
rest of the list which I´m suscribed to, send  the mail replies to the list
automatically, but this doesn´t. Please, can this be changed?.

-- Forwarded message --
From: Alberto G. Corona agocor...@gmail.com
Date: 2009/6/13
Subject: Re: [Haskell-cafe] curious about sum
To: Deniz Dogan deniz.a.m.do...@gmail.com


first, I was completely wrong. It is foldl what is neccesary. sum is
defined in terms of foldl:

sum= foldl (+) 0

but
Prelude foldl (+)  0 [1..100]
*** Exception: stack overflow

that is not because + is non strict, but because foldl is:

foldl f z0 xs0 = lgo z0 xs0
where
   lgo z [] =  z
   lgo z (x:xs) = lgo (f z x) xs

this version of foldl IS strict:

foldlStrict f z0 xs0 = lgo z0 xs0
where
   lgo z [] =  z
   lgo z (x:xs) =let t= f z x in t `seq` lgo t xs

main= print $  foldlStrict (+) 0 [1..100]
5050

so the garbage collector do the job in freeing the consumed part of the
list.

2009/6/13 Alberto G. Corona agocor...@gmail.com

 Prelude let strictplus x y= let z=x +y in z `seq` z; sum1= foldr
 strictplus 0 in sum1[0..100]
 *** Exception: stack overflow
 I suppose that strictplus is strict, so the garbage collector would free
the consumed part of the list.
 Then, why the stack overflow?
 2009/6/13 Deniz Dogan deniz.a.m.do...@gmail.com

 2009/6/13 Jochem Berndsen joc...@functor.nl:
  Keith Sheppard wrote:
  Is there any reason that sum isn't strict? I can't think of any case
  where that is a good thing.
 
  Prelude sum [0 .. 100]
  *** Exception: stack overflow
 
  It is useful if the (+) is nonstrict; although I cannot think of any
  useful mathematical structure where (+) would be nonstrict.

 I remember needing a non-strict sum at least once, but I do not
 remember the exact application. But imagine having a (very) long list
 of numbers and you want to do A if the sum exceeds a small number,
 otherwise B.

 if sum [0..10]  10 then A else B

 However, this idea didn't work, because of strictness.

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

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


Re: Fwd: [Haskell-cafe] curious about sum

2009-06-14 Thread Jochem Berndsen
Alberto G. Corona wrote:
 Once more I forgot to send my messages to the haskell cafe list. All the
 rest of the list which I´m suscribed to, send  the mail replies to the list
 automatically, but this doesn´t. Please, can this be changed?.

This comes up every so often, but I would be against this. Your e-mail
client should support mailing lists.

 this version of foldl IS strict:
 
 foldlStrict f z0 xs0 = lgo z0 xs0
 where
lgo z [] =  z
lgo z (x:xs) =let t= f z x in t `seq` lgo t xs
 
 main= print $  foldlStrict (+) 0 [1..100]
 5050
 
 so the garbage collector do the job in freeing the consumed part of the
 list

This is correct; the function you defined is equivalent to foldl' in
Data.List, if I'm not mistaken.

Regards,
-- 
Jochem Berndsen | joc...@functor.nl
GPG: 0xE6FABFAB
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe