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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:45:07+00:00 2026-06-03T07:45:07+00:00

I am in trouble with ocaml. I want to make a function that will

  • 0

I am in trouble with ocaml.

I want to make a function that will increment my counter every time I call it and concat my vargen string with the counter number and return this new string.

What I did without success is:

let (counter : int ref) = ref 0;;
let (vargen : string) = "_t";;
let tmp = incr counter;ref (vargen ^ string_of_int !counter);;
Printf.printf "%s\n" !tmp;;
Printf.printf "%s\n" !tmp;;
Printf.printf "%s\n" !tmp;;
Printf.printf "%s\n" !tmp;;

But my output are always:

_t1
_t1
_t1
_t1

And what my output should be:

    _t0
    _t1
    _t2
    _t3

Any ideas to solve my problem guyz?

Thks all.

  • 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-03T07:45:09+00:00Added an answer on June 3, 2026 at 7:45 am

    When you write let tmp = ref foo, the expression foo is evaluated once, to produce a value which is stored in the reference. Accessing the reference returns this value, without re-evaluating the original expression.

    The way to provoke re-evaluation is to use a function instead: if you write a function (fun () -> foo), this is a value: it is returned as is, passed to functions, stored in references. And each time you apply an argument to this value, the expression foo is evaluated.

    Clément’s solution is good. The idea of

    let counter =
      let count = ref (-1) in
      fun () -> incr count; !count
    

    Is that the reference is allocated once, but incremented each time the function fun () -> incr count; !count is called. Having the reference local to the function avoids some of the pitfall of global variables. You can think of it as a “static variable” of the function counter, only it is a natural result of OCaml scoping and evaluation rules and not an additional, function-specific concept.

    You can even write an even more general vargen generator, that creates fresh, independent counters each time it’s called:

    let make_vargen prefix =
       let count = ref (-1) in
       fun () ->
         incr count;
         prefix ^ string_of_int !count
    
    let fresh_t = make_vargen "t"
    let () = print_endline (fresh_t ())  (* t0 *)
    let () = print_endline (fresh_t ())  (* t1 *)
    let fresh_u = make_vargen "u"
    let () = print_endline (fresh_u ())  (* u0 *)
    let () = print_endline (fresh_t ())  (* t2 *)
    let () = print_endline (fresh_u ())  (* u1 *)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create an OCaml function rv that will go through a
My trouble is that I want my WPF ListView to display ListViewItems in different
I would like to write an OCaml module having a compile function which will
Having trouble with query results. The getSales() function works great the first time it
Having trouble with proper regex for RewriteCond RewriteCond %{REQUEST_URI} !^/foo/ Works as expected, that
Having trouble with a mysql grant statement. I want a user who has: readonly
how can I manage to define a Set in OCaml that can contains element
Having trouble with pattern and replacement. How can I make the replacement echo a
Having trouble with a function hitting the page as soon as possible, so i
Strange trouble I want to upload image via ajax which many plugin does but

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.