While already posted answers are great for why your published code is not working, I would say that using a loop and a mutable variable is not very ... functional. Therefore, I thought that I would post more F # - an idiomatic way to solve it.
, " .NET". , , - .NET. , , , F #.
fold, , . Seq.fold, , :
let rec fold accFn arr acc =
match arr with
| [||] -> acc
| _ -> fold accFn arr.[1..] (accFn arr.[0] acc)
, accFn , . , .
, , fold:
let min x y =
if x < y then x
else y
let max x y =
if x > y then x
else y
let sum x y =
x + y
, :
let arrX= [|9; 11; 3; 4; 5; 6; 7; 8|]
let head = arrX.[0]
let avg = (fold sum arrX 0) / arrX.Length
let minValue = fold min arrX head
let maxValue = fold max arrX head