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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:53:43+00:00 2026-05-26T12:53:43+00:00

If you have a recursive function that relies on some other function what is

  • 0

If you have a recursive function that relies on some other function what is the preferred way to implement that?

1) outside the recursive function

let doSomething n = ...
let rec doSomethingElse x =
    match x with
    | yourDone -> ...
    | yourNotDone -> doSomethingElse (doSomething x)

2) inside the recursive function

let rec doSomethingElse x =
    let doSomething n = ...
    match x with
    | yourDone -> ...
    | yourNotDone -> doSomethingElse (doSomething x)

3) encapsulate both inside the a third function

let doSomethingElse x =
    let doSomething n = ...
    let innerDoSomethingElse =
        match x with
        | yourDone -> ...
        | yourNotDone -> innerDoSomethingElse (doSomething x)

4) something even better?

  • 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-26T12:53:44+00:00Added an answer on May 26, 2026 at 12:53 pm
    module Test =
    
        let f x = 
          let add a b = a + b //inner function
          add x 1
    
        let f2 x =
          let add a = a + x //inner function with capture, i.e., closure
          add x
    
        let outerAdd a b = a + b
    
        let f3 x =
          outerAdd x 1
    

    Translates to:

    [CompilationMapping(SourceConstructFlags.Module)]
    public static class Test {
    
        public static int f(int x) {
            FSharpFunc<int, FSharpFunc<int, int>> add = new add@4();
            return FSharpFunc<int, int>.InvokeFast<int>(add, x, 1);
        }
    
        public static int f2(int x) {
            FSharpFunc<int, int> add = new add@8-1(x);
            return add.Invoke(x);
        }
    
        public static int f3(int x) {
            return outerAdd(x, 1);
        }
    
        [CompilationArgumentCounts(new int[] { 1, 1 })]
        public static int outerAdd(int a, int b) {
            return (a + b);
        }
    
        [Serializable]
        internal class add@4 : OptimizedClosures.FSharpFunc<int, int, int> {
            internal add@4() { }
    
            public override int Invoke(int a, int b) {
                return (a + b);
            }
        }
    
        [Serializable]
        internal class add@8-1 : FSharpFunc<int, int> {
            public int x;
    
            internal add@8-1(int x) {
                this.x = x;
            }
    
            public override int Invoke(int a) {
                return (a + this.x);
            }
        }
    }
    

    The only additional cost for an inner function is new’ing up an instance of FSharpFunc–seems negligible.

    Unless you’re very performance sensitive, I would go with the scope that makes the most sense, that is, the narrowest scope possible.

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

Sidebar

Related Questions

I have a simple recursive function RCompare() that calls a more complex function Compare()
Is it possible to have a PHP function that is both recursive and anonymous?
I have some sort of recursive function, but I need to parse a string,
I have a recursive scalar function that needs to update a record in another
Say I have a recursive function that I want to know how many times
I have a simple recursive function that calculates a simple pendulum swing decay, using
I have a simple recursive array function that looks like this: function recursive_array($results) {
Let's say I have some data that I need to get from the server
I have a recursive function that is supposed to print the contents of a
I have a simple recursive function that is returning undefined instead of the desired

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.