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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:13:52+00:00 2026-05-15T16:13:52+00:00

Firstly, apologies for the vague title, but I’m not sure exactly what I’m asking

  • 0

Firstly, apologies for the vague title, but I’m not sure exactly what I’m asking here(!).

After encountering Haskell at university, I’ve recently started using it in anger and so am working through the Project Euler problems as an extended Hello World, really. I’ve encountered a bug in one of my answers that seems to suggest a misunderstanding of a fundamental part of the language, and it’s not something I could work out from the tutorials, nor something I know enough about to start Googling for.

A brief description of the issue itself – the solution relates to primes, so I wanted an infinite list of prime numbers which I implemented (without optimisation yet!) thusly:

isPrime :: Int -> Bool
isPrime n = isPrime' 2 n
            where isPrime' p n | p >= n           = True
                               | n `mod` p == 0  = False
                               | otherwise       = isPrime' (p+1) n

primes :: [Int]
primes = filter isPrime [2..]

Since infinite lists can be a little tedious to evaluate, I’ll of course be using lazy evaluation to ensure that just the bits I want get evaulatued. So, for example, I can ask GHCI for the prime numbers less than 100:

*Main> takeWhile (< 100) primes
[2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97]

Now here’s the part that I don’t understand at all – when the upper limit gets large enough, I get no answers back at all. In particular:

*Main> takeWhile (< 4000000000) primes
[]

This isn’t a problem with takeWhile itself, or the partially-applied function, as takeWhile (< 4000000000) [2..] works as I would expect. It’s not a problem with my use of filter (within the definition of primes), since takeWhile (< 4000000000) (filter even [2..]) also returns the expected result.

Through binary search I found that the greatest upper limit that works is 2^31 - 1, so this would certainly seem to imply some kind of space-based constraint (i.e. largest positive signed integer). However:

  1. I was of the impression that Haskell had no language limits on the size of integers, and they were bounded only by the amount of free memory.
  2. This number only appears in the less-than predicate, which I know works as expected in at least some cases. Surely when it’s applied to elements of a list, it shouldn’t care where they come from? Looking solely at the first element, I know that the predicate returns true for the input 2 when it comes from filter even [2..]; I know that primes returns 2 as its first element. So how can my list be empty, how is this predicate failing “for some values of 2”?

Any thoughts would be grateful as I don’t have enough experience to know where to start with this one. Thanks for taking the time to take a look.

  • 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-15T16:13:53+00:00Added an answer on May 15, 2026 at 4:13 pm

    There are 2 built-in integral types in haskell: Int and Integer. Integer is the default and is unbounded. Int however is bounded. Since you’re explicitly using Int in the type for isPrime 4000000000 is used as an Int and overflows. If you change the type of isPrime to Integer -> Bool or even better Integral a => a -> Bool (read: a function that can take any kind of Integral value and returns a Bool), it will work as expected.

    The important thing to take away here (other than the difference between Int and Integer) is that the type of 4000000000 depends on how it is used. If it is used as an argument to a function that takes an Int, it will be an Int (and on 32-bit systems it will overflow). If it is used as an argument to a function that takes an Integer, it will be an Integer (and never overflow). If it is used as an argument to a function that takes any kind of Integral, it will also be an Integer because Integer is the default instance of Integral.

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

Sidebar

Related Questions

Firstly i apologize for vague question title, i am not sure what to write
Firstly, apologies for the bad question title - not entirely sure if I am
Firstly, apologies for the title, I could not think of a suitable one. I
Firstly appologies for the poor title, not sure how to explain this in one
Firstly: I get the mvc and php of magento, but I'm not fully versed
Firstly apologies for the title, I don't know if it describes what I am
Firstly apologies if this is really simple but I have spent hours trying and
Firstly apologies for the length of this question, and for asking about the Facebook
Firstly apologies I've re-edited this as the original was so vague. What I have
Firstly apologies if this is a really simple question but Git is absolutely brand

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.