English 中文(简体)
在Ocaml保持休养电话次数
原标题:Keeping a count of the number of recursive calls in Ocaml

I m new to Ocaml. 问题I m正在跟踪一项职能中的休养呼吁的数量。

例如,我写道:

    let rec someFunction n = 
    let digi = someOtherFunction n in
    match digi with
    | x when x > 123 -> someFunction digi 
    | x -> [want to output # of recursive calls] ;;

我如何这样做? 我试图制造一个变数,但是如果我有某种功能的话,它只是继续重新确定其最初的价值。 我基本上想要做的是:

     while ( x > 123) {
     count++;
     someFunction(x);
     }

     return count;

如果这是极其微乎其微的。

最佳回答

首先,我对你重新试图与反面做些什么感到困惑不解,你们是否希望它达到123? 意味着:

while (count < 123) { count++; someFunction(count); }

......意味着,在达到123岁以前,你需要多少时间才能离开。

如果你想要保留多少职能,直到达到某种限制,那么你就可采用这样的办法:

let someFunction n =
  let count = ref 0 in
  let rec aux () = 
    if !count >= n then count
    else (
      incr count;
      (* do the stuff you wanted to do in someFunction here *) 
      aux () ) in
  aux () ;;

如果你想避免 state的国家(一般是一个好主意),那么你可以这样做,而不会 re:

let someFunction n  =
  let rec aux count = 
      if count >= n then count
      else  aux (count+1)  in
  aux 0 ;;

或许这是你试图做些什么?

let someOtherFunction n = 
  Printf.printf "n is: %d
" n;;

let someFunction n f =
  let rec aux count = 
    if count >= n then count
    else (
      f count;
      aux (count+1)  
    ) in
  aux 0 ;;

# someFunction 10 someOtherFunction ;;
n is: 0
n is: 1
n is: 2
n is: 3
n is: 4
n is: 5
n is: 6
n is: 7
n is: 8
n is: 9
- : int = 10

而另一方面,如果你想要跟踪多少次职能,那么你就不得不在与某些职能定义相同的程度上抵制,例如:

let count = ref 0 ;;
let rec someFunction n f = 
  if !count >= 123 then count
  else (
    incr count;
    f n;
    someFunction n f
  ) ;;
问题回答

这样做有几个途径。 一种做法是,与你写的正文一样,在提及允许变数能够改变价值的同时,这样做。

let x := starting_value;
let count := 0;
while ( !x > 123) do (
     count := count + 1;
     x := someFunction(x)
     ) done;
!count

Or if you want to write purely functional code, you could add a helper function like so:

let someFunction n = 
    let rec someFunctionHelper n count = 
        let digi = someOtherFunction n in
        match digi with
        | x when x > 123 -> someFunctionHelper digi (count + 1)
        | x -> count
    in 
        someFunctionHelper n 0

The second way is how I would write it. Essentially, we re just replacing someFunction from your original code with an alternate version which takes an extra argument which indicates how many times it has been called so far. When we call it the first time (last line of the function) we start it at zero and then after that, we pass a number one higher than what we got each time.





相关问题
ocamlc, module compilation

I wrote an app in ocaml. It consist of several modules: Util (util.ml) Work1 (work1.ml) -- open Util Work2 (work2.ml) -- open Util, too Main (main.ml) -- open all of them. When i compile its, using ...

How can I simplify this ocaml pattern-matching code?

I m writing a simple little ocaml program that reads an algebraic statement in from a file, parses it into an AST using ocamllex/ocamlyacc, reduces it, and then prints it. The part where I m reducing ...

How can I create a type with multiple parameters in OCaml?

I m trying to create a type that has multiple type parameters. I know how to make a type with one parameter: type a foo = a * int But I need to have two parameters, so that I can parameterize the ...

Hashtable indexed on several fields

I m currently programming an OCaml module defining a type corresponding to a CPU register. The interface of this module is the following : (* * Defines a type which represents a R3000 register. *) ...

Extending an existing type in OCaml

I ve been doing some OCaml programming lately to learn the language and to get more acquainted with functional programming. Recently, I ve started to think that I d like to be able to extend an ...

Ocaml Syntax Error

What s wrong with this code? I can t figure it out: let parent (rules : grammar) (symbol1 : string) (symbol2 : string) : (SymbolSet.t) = try SymbolSet.singleton (getParent [symbol1; symbol2] ...

热门标签