2
\$\begingroup\$

I have code that zips 3 lists, finds a maximum and extracts part of the max tuple. Isn't there a shorter way to do this in F#?

let Triplets = List.zip3 A B C
let T1 (x, _, _) = x
let T2 (_, x, _) = x
let T3 (_, _, x) = x
let Best = List.maxBy T3 Triplets
T1 Best,T2 Best // return to C# code
\$\endgroup\$
1
  • \$\begingroup\$ You could obviously combine T1 and T2 to T12 (x, y, _) = (x, y). The rest of your solution looks pretty idiomatic to me. However, the need of such "zip3-max-extract" operation chain could be a sign for a design flaw somewhere else in your logic. \$\endgroup\$
    – conrad
    Mar 1, 2015 at 15:40

1 Answer 1

3
\$\begingroup\$

One way to simplify this would be to use nested pairs (('a * 'b) * 'c) instead of a triple ('a * 'b * 'c). That way, you can use fst and snd to get to the parts that you want:

let triplets = List.zip (List.zip A B) C
let best = List.maxBy snd triplets
fst best

Notice that I also changed the names of variables to lowercase, that is the common naming convention in F#.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.