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

  • Home
  • SEARCH
  • 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 7158445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:04:02+00:00 2026-05-28T13:04:02+00:00

As a newbie to Haskell I am trying to iterate a function (e.g., the

  • 0

As a newbie to Haskell I am trying to iterate a function (e.g., the logistic map) a large number of times. In an imperative language this would be a simple loop, however in Haskell I end up with stack overflow. Take for example this code:

main  = print $ iter 1000000

f x = 4.0*x*(1.0-x)

iter :: Int -> Double
iter 0 = 0.3
iter n = f $ iter (n-1)

For a small number of iterations the code works, but for a million iterations I get a stack space overflow:

Stack space overflow: current size 8388608 bytes.
Use `+RTS -Ksize -RTS' to increase it.

I cannot understand why this does happen. The tail recursion should be fine here.
Maybe the problem is lazy evaluation. I experimented with several ways to force strict evaluation, by inserting $! or seq at various positions, but with no success.

What would be the Haskell way to iterate a function a huge number of times?

I have tried suggestions from related posts: here or here, but I always ended up with stackoverflow for a large number of iterations, e.g., main = print $ iterate f 0.3 !! 1000000.

  • 1 1 Answer
  • 0 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-28T13:04:02+00:00Added an answer on May 28, 2026 at 1:04 pm

    The problem is that your definition

    iter :: Int -> Double
    iter 0 = 0.3
    iter n = f $ iter (n-1)
    

    tries to evaluate in the wrong direction. Unfolding it for a few steps, we obtain

    iter n = f (iter (n-1))
           = f (f (iter (n-2)))
           = f (f (f (iter (n-3))))
           ...
    

    and the entire call stack from iter 1000000 to iter 0 has to be built before anything can be evaluated. It would be the same in a strict language. You have to organise it so that part of the evaluation can take place before recurring. The usual way is to have an accumulation parameter, like

    iter n = go n 0.3
      where
        go 0 x = x
        go k x = go (k-1) (f x)
    

    Then adding strictness annotations – in case the compiler doesn’t already add them – will make it run smoothly without consuming stack.

    The iterate variant has the same problem as your iter, only the call stack is built inside-out rather than outside-in as for yours. But since iterate builds its call-stack inside-out, a stricter version of iterate (or a consumption pattern where earlier iterations are forced before) solves the problem,

    iterate' :: (a -> a) -> a -> [a]
    iterate' f x = x `seq` (x : iterate' f (f x))
    

    calculates iterate' f 0.3 !! 1000000 without problem.

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

Sidebar

Related Questions

I'm a complete newbie at Haskell. I'm trying to compile this Haskell file I've
I am an absolute newbie in Haskell yet trying to understand how it works.
To Rephrase I am newbie to haskell. I am trying to parse a pcap
I'm a haskell newbie and I couldn't find an answer to this question. Can
I am a newbie in Haskell and hope this question is not silly. I
Newbie here with a frustrating, but probably simple question. I am trying to write
Newbie question. Why is this JavaScript function returning undefined? var redis = require(redis), client
I'm a Haskell newbie, trying to accomplish a Caesar cipher exercise. In a .hs
Haskell newbie here. I wrote an evaluator for a minimal assembly-like language. Now, I
i m a newbie to haskell, currently i need a function 'f' which, given

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.