I am trying to get the mean of a list by using the following helping function:
let rec foldl f l b = match l with
| [] -> b
| x :: l -> foldl f l (f x b);;
let avg l = foldl (fun x acc -> let x = (a,b) in avv = (a +. acc) /. (b + 1)) l 0;;
But it keeps giving me errors after error
Need a corrected version of it
One simple way to implement a correct average is to implement it in terms of
sumandlen(gth), both of which are straightforward to write in terms offoldl:(we could also make
len lreturn a float by using+.,1.0and0.0instead of casting usingfloat_of_int, but that wouldn't make much sense)You seem to have tried to keep track of both the current length and the current sum at the same time, using a tuple, so let's do that too: