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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T22:38:16+00:00 2026-05-12T22:38:16+00:00

I have trouble understanding this chunk of code: let sieve (p:xs) = p :

  • 0

I have trouble understanding this chunk of code:

let
  sieve (p:xs) = p : sieve (filter (\ x -> x `mod` p /= 0) xs)
in sieve [2 .. ]

Can someone break it down for me? I understand there is recursion in it, but thats the problem I can’t understand how the recursion in this example works.

  • 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-12T22:38:17+00:00Added an answer on May 12, 2026 at 10:38 pm

    It’s actually pretty elegant.

    First, we define a function sieve that takes a list of elements:

    sieve (p:xs) =
    

    In the body of sieve, we take the head of the list (because we’re passing the infinite list [2..], and 2 is defined to be prime) and append it (lazily!) to the result of applying sieve to the rest of the list:

    p : sieve (filter (\ x -> x 'mod' p /= 0) xs)
    

    So let’s look at the code that does the work on the rest of the list:

    sieve (filter (\ x -> x 'mod' p /= 0) xs)
    

    We’re applying sieve to the filtered list. Let’s break down what the filter part does:

    filter (\ x -> x 'mod' p /= 0) xs
    

    filter takes a function and a list on which we apply that function, and retains elements that meet the criteria given by the function. In this case, filter takes an anonymous function:

    \ x -> x 'mod' p /= 0
    

    This anonymous function takes one argument, x. It checks the modulus of x against p (the head of the list, every time sieve is called):

     x 'mod' p
    

    If the modulus is not equal to 0:

     x 'mod' p /= 0
    

    Then the element x is kept in the list. If it is equal to 0, it’s filtered out. This makes sense: if x is divisible by p, than x is divisible by more than just 1 and itself, and thus it is not prime.

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

Sidebar

Related Questions

I have real trouble understanding mod_rewrite, and got some help in creating a RewriteRule
I have some functions in my code that accept either an object or an
For our assignment we need to write code for a neural network. The way
I have just started reading through The C Programming Language and I am having
Say I have a C program that in pseudoish is: For i=0 to 10
I have created a table that has a strongly type XML column in SQL2008
From what I have read on the web, one of the most efficient algorithms
I am new to Android development and have been following the tutorials available on
I would like to grab the prices of products from newegg. heres an example
I'm a bit new to database programming in general, and even newer to Entity

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.