Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Recursion 具有两个结果的递归_Recursion_F# - Fatal编程技术网

Recursion 具有两个结果的递归

Recursion 具有两个结果的递归,recursion,f#,Recursion,F#,我已经构建了一个递归计算两个的累进幂的函数,直到某个值n let rec aux_pow = fun e n -> match n with | n -> 1 # I tried with e, but clearly it modifies the result | _ -> 2 * aux_pow (e + 1) n 该函数将返回一个整数(2,4,8,16…n),其他函数可以使用该整数作为参数等。

我已经构建了一个递归计算两个的累进幂的函数,直到某个值
n

let rec aux_pow =
fun e n -> 
    match n with
    | n         ->  1        # I tried with e, but clearly it modifies the result
    | _         ->  2 * aux_pow (e + 1) n
该函数将返回一个整数(2,4,8,16…n),其他函数可以使用该整数作为参数等。容易的。我要寻找的是一种返回
e
值的方法,即功率等级。真的有可能吗?我知道每个函数只能返回一个值。获取递归的结果,以及在这种情况下,获取递归循环的数量的最佳方法是什么


例如,对于
aux_pow 0 8
,如何使
e
的值等于
3
,同时使函数返回
2*2*2=8
的结果?可能吗?这是一个没有意义的问题吗?

从函数返回2个值的最简单方法是使用。我对您提供的示例代码做了一些修改,并提出了一个可以满足您需要的版本

let aux_pow limit =
  let rec aux_pow' value counter =
    match value with
    | n when n = limit -> (limit, counter)
    | n when n > limit -> (int (2. ** double (counter - 1)), counter - 1)
    | _ -> aux_pow' (2 * value) (counter + 1)

  aux_pow' 1 0
它定义了一个内部递归函数,用于记录其运行的次数。输出是一个具有值和计数的元组


它还包含为限制指定非2次幂值的情况。

从函数返回2个值的最简单方法是使用。我对您提供的示例代码做了一些修改,并提出了一个可以满足您需要的版本

let aux_pow limit =
  let rec aux_pow' value counter =
    match value with
    | n when n = limit -> (limit, counter)
    | n when n > limit -> (int (2. ** double (counter - 1)), counter - 1)
    | _ -> aux_pow' (2 * value) (counter + 1)

  aux_pow' 1 0
它定义了一个内部递归函数,用于记录其运行的次数。输出是一个具有值和计数的元组

它还包含为限制指定非2次方值的情况