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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:43:12+00:00 2026-05-15T17:43:12+00:00

I am new to F# and was reading about tail recursive functions and was

  • 0

I am new to F# and was reading about tail recursive functions and was hoping someone could give me two different implementations of a function foo – one that is tail recursive and one that isn’t so that I can better understand the principle.

  • 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-15T17:43:13+00:00Added an answer on May 15, 2026 at 5:43 pm

    Start with a simple task, like mapping items from ‘a to ‘b in a list. We want to write a function which has the signature

    val map: ('a -> 'b) -> 'a list -> 'b list
    

    Where

    map (fun x -> x * 2) [1;2;3;4;5] == [2;4;6;8;10]
    

    Start with non-tail recursive version:

    let rec map f = function
        | [] -> []
        | x::xs -> f x::map f xs
    

    This isn’t tail recursive because function still has work to do after making the recursive call. :: is syntactic sugar for List.Cons(f x, map f xs).

    The function’s non-recursive nature might be a little more obvious if I re-wrote the last line as | x::xs -> let temp = map f xs; f x::temp — obviously its doing work after the recursive call.

    Use an accumulator variable to make it tail recursive:

    let map f l =
        let rec loop acc = function
            | [] -> List.rev acc
            | x::xs -> loop (f x::acc) xs
        loop [] l
    

    Here’s we’re building up a new list in a variable acc. Since the list gets built up in reverse, we need to reverse the output list before giving it back to the user.

    If you’re in for a little mind warp, you can use continuation passing to write the code more succinctly:

    let map f l =
        let rec loop cont = function
            | [] -> cont []
            | x::xs -> loop ( fun acc -> cont (f x::acc) ) xs
        loop id l
    

    Since the call to loop and cont are the last functions called with no additional work, they’re tail-recursive.

    This works because the continuation cont is captured by a new continuation, which in turn is captured by another, resulting in a sort of tree-like data structure as follows:

    (fun acc -> (f 1)::acc)
        ((fun acc -> (f 2)::acc)
            ((fun acc -> (f 3)::acc)
                ((fun acc -> (f 4)::acc)
                    ((fun acc -> (f 5)::acc)
                        (id [])))))
    

    which builds up a list in-order without requiring you to reverse it.


    For what its worth, start writing functions in non-tail recursive way, they’re easier to read and work with.

    If you have a big list to go through, use an accumulator variable.

    If you can’t find a way to use an accumulator in a convenient way and you don’t have any other options at your disposal, use continuations. I personally consider non-trivial, heavy use of continuations hard to read.

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

Sidebar

Related Questions

I've been doing some reading about two (relatively) new concepts in the Javascript language
I am new to C and I was reading about how pointers point to
I've just been reading about MSDeploy, the new website deployment tool from Microsoft. I'm
I am completely new to HTML5 and have been reading about it for the
I am new at MVC in Asp.Net and while i am reading about the
I am very new to programming in general, while reading about PHP I saw
I am fairly new to Java, and recently I was reading some material about
I'm very new to flash and actionscript 3. I've been reading a lot about
I'm fairly new to DDD world and after reading couple of books about it
I've been reading about the new native client support for chrome and I was

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.