\$\begingroup\$
\$\endgroup\$
1
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
svick
24.5k4 gold badges53 silver badges89 bronze badges
1 Answer 1
\$\begingroup\$
\$\endgroup\$
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#.
answered Mar 1, 2015 at 16:09
lang-ml
T1
andT2
toT12 (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\$