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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:07:16+00:00 2026-05-27T10:07:16+00:00

To help me learn Haskell, I am working through the problems on Project Euler.

  • 0

To help me learn Haskell, I am working through the problems on Project Euler. After solving each problem, I check my solution against the Haskell wiki in an attempt to learn better coding practices. Here is the solution to problem 3:

primes = 2 : filter ((==1) . length . primeFactors) [3,5..]

primeFactors n = factor n primes
  where
    factor n (p:ps) 
        | p*p > n        = [n]
        | n `mod` p == 0 = p : factor (n `div` p) (p:ps)
        | otherwise      = factor n ps

problem_3 = last (primeFactors 317584931803)

My naive reading of this is that primes is defined in terms of primeFactors, which is defined in terms of primes. So evaluating primeFactors 9 would follow this process:

  1. Evaluate factor 9 primes.
  2. Ask primes for its first element, which is 2.
  3. Ask primes for its next element.
  4. As part of this process, evaluate primeFactors 3.
  5. Ask primes for its first element, which is 2.
  6. Ask primes for its next element.
  7. As part of this process, evaluate primeFactors 3.
  8. …

In other words, steps 2-4 would repeat infinitely. Clearly I am mistaken, as the algorithm terminates. What mistake am I making here?

  • 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-27T10:07:16+00:00Added an answer on May 27, 2026 at 10:07 am

    primeFactors only ever reads up to the square root of the number it’s evaluating. It never looks further in the list, which means it never “catches up” to the number it’s testing for inclusion in the list. Because Haskell is lazy, this means that the primeFactors test does terminate.

    The other thing to remember is that primes isn’t a function that evaluates to a list each time you access it, but rather a list that’s constructed lazily. So once the 15th element has been accessed once, accessing it a second time is “free” (e.g. it doesn’t require any further calculation).

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

Sidebar

Related Questions

I'm new to haskell, and i read through and digested Learn You A Haskell
So I'm making a list of prime numbers to help me learn haskell using
I'm working on a small app to help learn cakephp 2.0. Its a simple
I have come up with a small project to help me learn Java EE,
I'm pretty new to WPF and using the MVVM design pattern. To help learn
Can anyone recommend tools or articles that help me to learn how to TDD
I need help, how can I learn this framework? Here's what I need to
I'm just starting to learn T-SQL and could use some help in understanding what's
Hi im making a small site to help me and friends learn languages. Typical
I've started to learn OO programming, but using the PHP language with the help

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.