2017-09-03 45 views
0

我不太明白下面的代码,它计算数字列表的平均值和标准偏差:用于计算均值和标准差的代码是如何工作的?

let stats l = 
    let rec helper rest n sum sum_squares = 
    match rest with 
    | [] -> let nf = float_of_int n in 
     (sum /. nf, sqrt (sum_squares /. nf)) 
    | h :: t -> 
     helper t (n+1) (sum+.h) (sum_squares +. (h*.h)) in 
    helper l 0 0.0 0.0;; 

例如:

​​

要其解释响应

val mean : float = 3. 
val sd : float = 3.3166247903554 

In

 helper t (n+1) (sum+.h) (sum_squares +. (h*.h)) in 
    helper l 0 0.0 0.0;; 

inhelper l 0 0.0 0.0是什么意思?

谢谢。

回答

1

定义分解成碎片这样本身

let stats l = 
    let rec helper rest n sum sum_squares = 
     (* Definition of helper *) 
    in 
    helper l 0 0.0 0.0 

关键字in并不意味着什么。它符合let。您所说的in与定义为helperlet一起使用。所以用英文说就是lethelper定义如下inhelper l 0 0.0 0.0

表达式helper l 0 0.0 0.0是对由let定义的helper函数的调用。

因此,在更简单的英语中,它将“将帮助器定义为以下函数,然后使用参数l 0 0.0 0.0调用帮助器。”

相关问题