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 8304903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T17:57:41+00:00 2026-06-08T17:57:41+00:00

This code was taken from the book Haskell Road to Logic, Math and Programming.

  • 0

This code was taken from the book “Haskell Road to Logic, Math and Programming”. It implements sieve of eratosthenes algorithm and solves Project Euler Problem 10.

sieve :: [Integer] -> [Integer]
sieve (0 : xs) = sieve xs
sieve (n : xs) = n : sieve (mark xs 1 n)
  where
    mark :: [Integer] -> Integer -> Integer -> [Integer]
    mark (y:ys) k m | k == m = 0 : (mark ys 1 m)
                    | otherwise = y : (mark ys (k+1) m)

primes :: [Integer]
primes = sieve [2..]

-- Project Euler #10
main = print $ sum $ takeWhile (< 2000000) primes

Actually it runs even slower then the naive prime test.
Can someone explain this behaivour?

I suspect it has something to do with iterating each element in the list in the mark function.

Thanks.

  • 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-06-08T17:57:43+00:00Added an answer on June 8, 2026 at 5:57 pm

    You are building up a quadratic number of unevaluated thunks using this algorithm. The algorithm relies on laziness so heavily, that this also is the reason why it doesn’t scale.

    Let’s walk through how it works, which hopefully should make the problem apparent. For simplicitly, let’s say that we want to print the elements of primes ad infinitum, i.e. we want to evaluate each cell in the list one after the other. primes is defined as:

    primes :: [Integer]
    primes = sieve [2..]
    

    Since 2 isn’t 0, the second definition of sieve applies, and 2 is added to the list of primes, and the rest of the list is an unevaluated thunk (I use tail instead of the pattern match n : xs in sieve for xs, so tail isn’t actually being called, and doesn’t add any overhead in the code below; mark is actually the only thunked function):

    primes = 2 : sieve (mark (tail [2..]) 1 2)
    

    Now we want the second primes element. So, we walk through the code (exercise for the reader) and end up with:

    primes = 2 : 3 : sieve (mark (tail (mark (tail [2..]) 1 2)) 1 3)
    

    Same procedure again, we want to evaluate the next prime…

    primes = 2 : 3 : 5 : sieve (mark (tail (tail (mark (tail (mark (tail [2..]) 1 2)) 1 3))) 1 5)
    

    This is starting to look like LISP, but I digress… Starting to see the problem? For each element in the primes list, an increasingly large thunk of stacks of mark applications have to be evaluated. In other words, for each element in the list, there has to be a check for whether that element is marked by any of the preceding primes, by evaluating each mark application in the stack. So, for n~=2000000, the Haskell runtime has to call functions resulting in a call stack with a depth of about … I don’t know, 137900 (let n = 2e6 in n / log n gives a lower bound)? Something like that. This is probably what causes the slow-down; maybe vacuum can tell you more (I don’t have a computer with both Haskell and a GUI right now).

    The reason why the sieve of Eratosthenes works in languages like C is that:

    1. You aren’t using an infinite list.
    2. Because of (1), you can mark the whole array before continuing with the next n, resulting in no call stack overheads at all.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This example is taken from the Linux System Programming book, page 88. Code: http://pastebin.com/mEfmHbPP
The following code is taken from Programming in Scala book by Martin Odersky et
I'm trying to compile this piece of code from the book The C Programming
I am using this code taken from here : import smtplib def prompt(prompt): return
I use this code which is taken from MVC futures and attach the Attribute
This is a code sample taken from the Google Maps API developer's guide. I'm
Is this possible to do the following in ExpressionEngine: (code taken from here )
I don't understand the rationale of this code, taken from javax.swing.event.EventListenerList docs: protected void
Given below is program for encrypting a string. I had taken this code from
In this statement, taken from the Pagerank source code: Pattern.compile(\\[.+?\\]); What does the pattern

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.