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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:41:24+00:00 2026-06-05T07:41:24+00:00

I recently started with F# and implemented a very basic recursive function that represents

  • 0

I recently started with F# and implemented a very basic recursive function that represents the Sieve of Eratosthenes. I came up with the following, working code:

static member internal SieveOfEratosthenesRecursive sequence accumulator =
    match sequence with
    | [] -> accumulator
    | head::tail -> let rest = tail |> List.filter(fun number -> number % head <> 0L)
                    let newAccumulator = head::accumulator
                    Prime.SieveOfEratosthenesRecursive rest newAccumulator

This function is not really memory efficient so I tried to eliminate the variables “rest” and “newAccumulator”. I came up with the following code

static member internal SieveOfEratosthenesRecursive sequence accumulator =
    match sequence with
    | [] -> accumulator
    | head::tail -> tail    |> List.filter(fun number -> number % head <> 0L) 
                            |> Prime.SieveOfEratosthenesRecursive (head::accumulator)

As far as I understand the tutorials I’ve read Prime.SieveOfEratosthenesRecursive will be called with the filtered tail as first parameter and a list consisting of head::accumulator as second one. However when I try to run the code with the reduced variable usage, the program gets trappen in an infinite loop. Why is this happening and what did I do wrong?

  • 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-05T07:41:25+00:00Added an answer on June 5, 2026 at 7:41 am

    As far as I understand the tutorials I’ve read Prime.SieveOfEratosthenesRecursive will be called with the filtered tail as first parameter and a list consisting of head::accumulator as second one.

    You have this backwards.

    In the first version, you’re passing rest then newAccumulator; in the second version, you’re effectively passing newAccumulator then rest. I.e., you’ve transposed the arguments.

    Prime.SieveOfEratosthenesRecursive (head::accumulator) is a partial function application wherein you’re applying (head::accumulator) as the first argument (sequence). This partial function application yields a unary function (expecting accumulator), to which you are passing (via |>) what is called rest in the first version of your code.

    Changing SieveOfEratosthenesRecursive‘s argument order is the easiest solution, but I would consider something like the following idiomatic as well:

    static member internal SieveOfEratosthenesRecursive sequence accumulator =
        match sequence with
        | [] -> accumulator
        | head::tail ->
            tail
            |> List.filter(fun number -> number % head <> 0L) 
            |> Prime.SieveOfEratosthenesRecursive <| (head::accumulator)
    

    or

    static member internal SieveOfEratosthenesRecursive sequence accumulator =
        let inline flipzip a b = b, a
        match sequence with
        | [] -> accumulator
        | head::tail ->
            tail
            |> List.filter(fun number -> number % head <> 0L) 
            |> flipzip (head::accumulator)
            ||> Prime.SieveOfEratosthenesRecursive
    

    FWIW, eliminating rest and newAccumulator as named variables here is not going to impact your memory usage in the slightest.

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

Sidebar

Related Questions

I was working on some code recently and came across a method that had
I recently started using lcov to visualize my code coverage. It's a great tool.
Ive recently started using CodeIgniter as I was searching for a very lightweight framework
I recently started working with MVC or MVC2 to be more exact. I found
I have recently started a new job and noticed that all the SQL tables
Recently I started noticing a repetition in some of my code. Of course, once
I've recently started learning Scala and was disappointed (but not surprised) that their generics
I've recently started learning scala, and I've come across the :: (cons) function, which
I've recently started to work on an application based on the Kohana framework that
Recently started working with the scrapy library. I am trying to scrape from a

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.