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

  • Home
  • SEARCH
  • 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 6781199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:35:46+00:00 2026-05-26T16:35:46+00:00

The following code: let CreateFunc= let counter = ref 0 fun () -> counter

  • 0

The following code:

let CreateFunc=
    let counter = ref 0
    fun () -> counter := !counter + 1; !counter

let f1 = CreateFunc
let f2 = CreateFunc

printfn "%d" (f1())
printfn "%d" (f1())
printfn "%d" (f2())
printfn "%d" (f2())

Outputs:

1
2
3
4

So, basically, what we see here is f1 and f2 being the same function – as they’re obviously sharing the same instance of ‘counter’.

The expected output is:

1
2
1
2

QUESTION: Shouldn’t f1 and f2 be two separate instances? After all they are created by the two different calls to ‘CreateFunc’???

Thanks

  • 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-26T16:35:46+00:00Added an answer on May 26, 2026 at 4:35 pm
    let CreateFunc() =
        let counter = ref 0
        fun () -> counter := !counter + 1; !counter
    
    let f1 = CreateFunc()
    let f2 = CreateFunc()
    
    printfn "%d" (f1())
    printfn "%d" (f1())
    printfn "%d" (f2())
    printfn "%d" (f2())
    

    Output is

    1
    2
    1
    2
    

    Explanation:

    In your original solution, CreateFunc was a function, but always the same function (CreateFunc, f1 and f2 were all synonyms, all pointing to the same function). In my solution CreateFunc is a function which returns a new function whenever it is called, thus each function has its own state (i.e. counter).

    In short: the original CreateFunc was a value, always the same value.

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

Sidebar

Related Questions

Assume I have the following code: let a = ref 4. printfn 1) a
Let's say I have the following code snippet. function test(id) { alert(id); } testChild.prototype
Following code has been picked up from this blog function! Privatize() let priorMethod =
Consider the following code: let fnOption = Some (fun () -> ()) fnOption =
So..Let's say I have the following code snippet function Rectangle(w,h){ this.width = w; this.height
Given the following code: let bar = lazy( printfn bar ()) let foo =
Let say I have the following code $(p).bind(click, function(){ alert( $(this).text() ); }); When
Given the following code: let DisplayImpl logger data = data |> Seq.iter logger printfn
I have the following code to let the GUI respond to a change in
Consider the following code fragment: let t1 = async { return process1() } let

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.