Lists and Folding

Category:
Lists, Tuples and Options
Description: This sample shows simple uses of 'fold' to accumulate a result over a list
Code:
let ListSample5() =
let data = [("Cats",4);
("Dogs",5);
("Mice",3);
("Elephants",2)]
let count = List.fold (fun acc (nm,x) -> acc+x) 0 data
printfn "Total number of animals: %d" count

Execution Result:
Total number of animals: 14

Last edited Sep 14, 2011 at 2:50 AM by ttliu2000, version 1

Comments

No comments yet.