Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 416723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T18:30:31+00:00 2026-05-12T18:30:31+00:00

I have this fairly simple function to compute the mean of elements of a

  • 0

I have this fairly simple function to compute the mean of elements of a big list, using two accumulators to hold the sum so far and the count so far:

mean = go 0 0
    where
      go s l []     = s / fromIntegral l
      go s l (x:xs) = go (s+x) (l+1) xs

main = do
  putStrLn (show (mean [0..10000000]))

Now, in a strict language, this would be tail-recursive, and there would be no problem. However, as Haskell is lazy, my googling has led me to understand that (s+x) and (l+1) will be passed down the recursion as thunks. So this whole thing crashes and burns:

Stack space overflow: current size 8388608 bytes.

After further googling, I found seq and $!. Which it seems I don’t understand because all my attempts at using them in this context proved futile, with error messages saying something about infinite types.

Finally I found -XBangPatterns, which solves it all by changing the recursive call:

go !s !l (x:xs) = go (s+x) (l+1) xs

But I’m not happy with this, as -XBangPatterns is currently an extension. I would like to know how to make the evaluation strict without the use of -XBangPatterns. (And maybe learn something too!)

Just so you understand my lack of understanding, here’s what I tried (the only try that compiled, that is):

go s l (x:xs) = go (seq s (s+x)) (seq l (l+1)) xs

From what I could understand, seq should here force the evaluation of the s and l argument, thus avoiding the problem caused by thunks. But I still get a stack overflow.

  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T18:30:32+00:00Added an answer on May 12, 2026 at 6:30 pm

    I’ve written extensively on this:

    • Real World Haskell, ch 24: controlling evaluation
    • On recursion and strictness in Haskell

    Firstly, yes, if you want to require strict evaluation of the accumulators use seq and stay in Haskell 98:

    mean = go 0 0
      where
        go s l []     = s / fromIntegral l
        go s l (x:xs) = s `seq` l `seq`
                          go (s+x) (l+1) xs
    
    main = print $ mean [0..10000000]
    
    *Main> main
    5000000.0
    

    Secondly: strictness analysis will kick in if you give some type annotations, and compile with -O2:

    mean :: [Double] -> Double
    mean = go 0 0
     where
      go :: Double -> Int -> [Double] -> Double
      go s l []     = s / fromIntegral l
      go s l (x:xs) = go (s+x) (l+1) xs
    
    main = print $ mean [0..10000000]
    
    $ ghc -O2 --make A.hs
    [1 of 1] Compiling Main             ( A.hs, A.o )
    Linking A ...
    
    $ time ./A
    5000000.0
    ./A  0.46s user 0.01s system 99% cpu 0.470 total
    

    Because ‘Double’ is a wrapper over the strict atomic type Double#, with optimizations on, and a precise type, GHC runs strictness analysis and infers that the strict version will be ok.

    import Data.Array.Vector
    
    main = print (mean (enumFromToFracU 1 10000000))
    
    data Pair = Pair !Int !Double
    
    mean :: UArr Double -> Double   
    mean xs = s / fromIntegral n
      where
        Pair n s       = foldlU k (Pair 0 0) xs
        k (Pair n s) x = Pair (n+1) (s+x)
    
    $ ghc -O2 --make A.hs -funbox-strict-fields
    [1 of 1] Compiling Main             ( A.hs, A.o )
    Linking A ...
    
    $ time ./A
    5000000.5
    ./A  0.03s user 0.00s system 96% cpu 0.038 total
    

    As described in the RWH chapter above.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this formula in a function below. It's a fairly simple concept, yet
I'm sure this is fairly simple, however I have a major mental block on
Lets say I have a class A that is fairly simple like this -
I know this is fairly fundamental stuff. I have a class with a function
This seems like a fairly simple problem to me but I have been having
I have a problem building a fairly simple local CGO project. Consider this very
There is this simple function which I have used with C++ in the past
I have a fairly simple function in Delphi which takes a string and produces
I have a fairly simple class that looks like this: class Person { public:
this might be fairly simple but I cant find my way around... I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.