F# take a list of pairs and return a pair

987 views Asked by At

trying to take a list of pairs and return a pair that is the x values multiplied with the y values multipled: ex: mult [(x,y);(x,y);(x,y)] would be (xxx,yyy) mult [(1,2);((1,2);(1,2)] would return (1,8)

trying to do this with map fst and no recursion currently my code is:

let mult li = 
    match li with map fst li

fairly new to f#

1

There are 1 answers

0
Jarak On BEST ANSWER

If I'm understanding you correctly, you have a list of 2-tuples of numbers, and you want to return a single 2-tuple, where the first element represents the product of all the first elements of the list, and the second element likewise represents the product of all the second elements of the list. Correct?

The easiest way to do this would probably be something like:

let mult li = 
    let a,b = List.unzip li
    let product = List.fold (*) 1
    (product a, product b)

To the best of my knowledge, F# doesn't include a List.product higher-order function by default, so I create one for convenience. One thing to note about this product function is that it will return 1 if called on an empty list (since 1 is the starting value of it). If you need alternative behaviour, the best thing to do would be to pattern match on the list and do the fold on a normal list, but return a default value on an empty list. You could use List.reduce (*), but that instead will throw an exception on an empty list.

Testing this out in FSI with the input [(1,2);(1,2);(1,2)], I get the result (1,8).

If you wanted to use the fst and snd functions, then you could modify it to something like:

let mult li = 
    let a = List.map fst li
    let b = List.map snd li
    let product = List.fold (*) 1
    (product a, product b)

List.unzip is plain easier though (and probably more efficient, since I think it only needs to iterate over the list once)