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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T23:12:25+00:00 2026-06-04T23:12:25+00:00

There are a couple of questions about tail-recursive function e.g. this and this but

  • 0

There are a couple of questions about tail-recursive function e.g. this and this but could not find anything similar to the following.

My understanding is that a tail-call optimised function should return an accumulated value in its last call without any further evaluation. It’s quite easy to understand using factorial function, for example, which get optimized into loops 2. But it not always obvious to tell in other cases e.g. in the following, what is that last call? There are many of them as the function is called recursively more than once in the body.

Brian suggests a way of finding out but I am not sure how to make it tail-call optimised. I can pass the --tailcalls flag to the compiler to do it automatically but does it always succeed?

f and g returns the same type.

type T = T of int * T list

let rec myfunc f (T (x,xs)) =
    if (List.isEmpty xs) then f x
    else 
        List.fold g acc (List.map (fun xxs -> myfunc f xxs) xs)

Any help to tail-call optimise the above code would be much appreciated.

  • 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-04T23:12:27+00:00Added an answer on June 4, 2026 at 11:12 pm

    As Jon already said, your function is not tail-recursive. The basic problem is that it needs to call itself recursively multiple times (once for every element in the xs list, which is done in the lambda function passed to List.map).

    In case when you actually need to make multiple recursive calls, using the continuation passing style or i.e. imperative stack are probably the only options. The idea behind continuations is that every function will take another function (as the last argument) that should be executed when the result is available.

    The following example shows normal version (on the left) and continuation based (on the right)

    let res = foo a b                          fooCont a b (fun res ->
    printfn "Result: %d" res                     printfn "Result: %d" res)
    

    To write your function in a continuation passing style, you’ll need to use a continuation-based fold function too. You can first avoid using map by moving the operation done in map into the lambda function of fold:

    List.fold g acc (List.map (fun xxs -> myfunc f xxs) xs)
    

    Becomes:

    List.fold (fun state xxs -> g state (myfunc f xxs)) acc xs
    

    Then you can rewrite the code as follows (Note that both f and g that you did not show in your question are now continuation-based functions, so they take additional argument, which represents the continuation):

    // The additional parameter 'cont' is the continuation to be called 
    // when the final result of myfunc is computed
    let rec myfunc' f (T (x,xs)) cont = 
      if (List.isEmpty xs) then 
        // Call the 'f' function to process the last element and give it the
        // current continuation (it will be called when 'f' calculates the result)
        f x cont
      else  
        // Using the continuation-based version of fold - the lambda now takes current
        // element 'xxs', the accumulated state and a continuation to call
        // when it finishes calculating 
        List.foldCont (fun xxs state cont -> 
          // Call 'myfunc' recursively - note, this is tail-call position now!
          myfunc' f xxs (fun res -> 
            // In the continuation, we perform the aggregation using 'g'
            // and the result is reported to the continuation that resumes
            // folding the remaining elements of the list.
            g state res cont)) acc xs cont
    

    The List.foldCont function is a continuation-based version of fold and can be written as follows:

    module List = 
      let rec foldCont f (state:'TState) (list:'T list) cont =
        match list with
        | [] -> cont state
        | x::xs -> foldCont f state xs (fun state ->
            f x state cont)
    

    Since you did not post a complete working example, I could not really test the code, but I think it should work.

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

Sidebar

Related Questions

I know there's a couple of questions about this out there but I still
There are already a couple of questions on finding cycles, but I did not
I know that there are couple of questions here about a countdown timer but
There have been a couple of questions about limiting login attempts, but none have
I have gone through a couple of similar questions but could reach a definite
I have a couple questions about SSL certificates. I never used them before but
I can find questions about zombies but none that directly addresses what they are
I have a couple questions about how to use C++ sets (std::set) Is there
There are many questions about events in interfaces. Here are a couple: Raising events
I am aware that there are a couple of questions that look similar to

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.