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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:05:02+00:00 2026-05-11T17:05:02+00:00

I wrote the follwing function: let str2lst str = let rec f s acc

  • 0

I wrote the follwing function:

let str2lst str =
    let rec f s acc =
      match s with
        | "" -> acc
        | _  -> f (s.Substring 1) (s.[0]::acc)
    f str []

How can I know if the F# compiler turned it into a loop? Is there a way to find out without using Reflector (I have no experience with Reflector and I Don’t know C#)?

Edit: Also, is it possible to write a tail recursive function without using an inner function, or is it necessary for the loop to reside in?

Also, Is there a function in F# std lib to run a given function a number of times, each time giving it the last output as input? Lets say I have a string, I want to run a function over the string then run it again over the resultant string and so on…

  • 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-11T17:05:02+00:00Added an answer on May 11, 2026 at 5:05 pm

    Edit: Since F# 8 there is a way with the [<TailCall>] attribute, see this answer: https://stackoverflow.com/a/77532717/969070

    Original answer:

    Unfortunately there is no trivial way.

    It is not too hard to read the source code and use the types and determine whether something is a tail call by inspection (is it ‘the last thing’, and not in a ‘try’ block), but people second-guess themselves and make mistakes. There’s no simple automated way (other than e.g. inspecting the generated code).

    Of course, you can just try your function on a large piece of test data and see if it blows up or not.

    The F# compiler will generate .tail IL instructions for all tail calls (unless the compiler flags to turn them off is used – used for when you want to keep stack frames for debugging), with the exception that directly tail-recursive functions will be optimized into loops. (EDIT: I think nowadays the F# compiler also fails to emit .tail in cases where it can prove there are no recursive loops through this call site; this is an optimization given that the .tail opcode is a little slower on many platforms.)

    ‘tailcall’ is a reserved keyword, with the idea that a future version of F# may allow you to write e.g.

    tailcall func args
    

    and then get a warning/error if it’s not a tail call.

    Only functions that are not naturally tail-recursive (and thus need an extra accumulator parameter) will ‘force’ you into the ‘inner function’ idiom.

    Here’s a code sample of what you asked:

    let rec nTimes n f x =
        if n = 0 then
            x
        else
            nTimes (n-1) f (f x)
    
    let r = nTimes 3 (fun s -> s ^ " is a rose") "A rose"
    printfn "%s" r
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote the function (defun test () (let ((str1 foo) (str2 bar)) (loop for
I can't seem to test a function I wrote in PLT Racket using the
I just wrote the following C++ function to programmatically determine how much RAM a
I wrote a managed C++ class that has the following function: void EndPointsMappingWrapper::GetLastError(char* strErrorMessage)
I wrote the following: Object.prototype.length = function(){ var count = -1; for(var i in
I want to call the Sleep function on ASM. So I wrote the following:
I'm learning sml, and wrote the following simple function: (* Return a list with
here's a function I wrote to print every digit of a float number in
Let's say I have a variadic function foo(int tmp, ...) , when calling foo
I need to convert all values in a structure (arguments passed into a function)

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.