[Haskell-cafe] curious about sum

Henk-Jan van Tuyl hjgtuyl at chello.nl
Wed Jun 17 12:22:51 EDT 2009


On Wed, 17 Jun 2009 13:32:40 +0200, Yitzchak Gale <gale at sefer.org> wrote:

> Henk-Jan van Tuyl wrote:
>> reverse
>> maximum
>> minimum
>
> Oh yes, please fix those also!

maximum' = foldl' max 0 [1 .. 999999]
minimum' = foldl' min 0 [1 .. 999999]

>
>> scanl
>> scanr
>> scanr1
>> iterate
>> take
>> drop
>> splitAt
>> inits
>
> Hmm, I use those all the time with large lists. They are lazy as  
> expected,
> and seem to work fine. Do you have examples of problems with them?

A hugs (version: Sep 2006) session:
Hugs> last $ scanl const 0 [0 .. 10 ^ 6]
ERROR - C stack overflow
Hugs> head $ scanr (+) 0 [1 .. 10 ^ 6]
ERROR - C stack overflow
Hugs> head $ scanr1 (+) [1 .. 10 ^ 6]
ERROR - C stack overflow
Hugs> iterate (+ 1) 0 !! (10 ^ 6)
ERROR - C stack overflow
Hugs> last $ take (10 ^ 6) [1 ..]
1000000
Hugs> head $ drop (10 ^ 6) [1 ..]
1000001
Hugs> head . snd $ splitAt (10 ^ 6) [1 ..]
1000001
Data.List> last $ last $ inits [1 .. 10 ^ 6]
ERROR - C stack overflow

A GHCi 6.10.1 session:
Prelude> last $ scanl const 0 [0 .. 10 ^ 6]
0
Prelude> head $ scanr (+) 0 [1 .. 10 ^ 6]
*** Exception: stack overflow
Prelude> head $ scanr1 (+) [1 .. 10 ^ 6]
*** Exception: stack overflow
Prelude> iterate (+ 1) 0 !! (10 ^ 6)
*** Exception: stack overflow
Prelude> last $ take (10 ^ 6) [1 ..]
1000000
Prelude> head $ drop (10 ^ 6) [1 ..]
1000001
Prelude> head . snd $ splitAt (10 ^ 6) [1 ..]
1000001
Prelude> :m Data.List
Prelude Data.List> last $ last $ inits [1 .. 10 ^ 6]
??? (not finished yet)

take, drop and splitAt seem to work fine now, but when I did these tests  
the first time, GHCi generated a stack overflow exception (I think it was  
GHCi 6.8.3).

>
>> foldM
>> filterM (Bulat)

  foldM' f a (x:xs)  =
    do
      a' <- f a x
      a' `seq` foldM' f a' xs


-- 
Met vriendelijke groet,
Henk-Jan van Tuyl


--
http://functor.bamikanarie.com
http://Van.Tuyl.eu/
--




More information about the Haskell-Cafe mailing list