How do I call the sum of a tuple list?

Viewed 93

I have made a tuple-list, but I can't seem to find the sum of the lists. Do I need to make a function for the sum, or can I call each list fst snd in the sum code?

let (startBoard : board) = ([0; 3; 3; 3; 3; 3], [0; 3; 3; 3; 3; 3])

How do I find the sum of the lists? I tried to make a sum function and to call fst and snd but can't seem to make it work...

let sum1 (fst : int list) : int = 
let s = List.sum fst

Hope I made my self clear :)

2 Answers

fst is a built-in function that returns the first item of a tuple of size two. Your example sum1 function is naming its first parameter as fst which is probably not what you want.

Try something like this:

let sum1 (startBoard : board) = List.sum (fst startBoard)

And with the power of currying, this can be shortened:

let sum1 = List.sum << fst

Don't know exactly what the 'board' type is, but this should work.

let sum (b: board) = List.sum (fst b), List.sum (snd b)
Related