我想通过前两个值在集合中找到元组并返回元组的第三个值(如果什么也没找到,则返回 None )。我想要这样的东西:
type Point = (int * int * int)
type Path = Set<Point>
let f (x:int) (y:int) (p:Path) : int Option =
if Set.exists ((=) (x, y, _z)) p
then Some _z
else None
let p:Path = Set.ofList [ (0, 1, 100); (1, 1, 500); (1, 2, 50); ]
f 1 2 p
但这不起作用,因为显然,表达式中不允许模式匹配。什么是正确的方法?谢谢。
最佳答案
您可以将集合转换为列表并使用 List.tryFind
let f (x:int) (y:int) (p:Path) : int Option =
Set.toList p
|> List.tryFind (fun (px, py, _) -> x = px && y = py)
|> Option.map (fun (_, _, pz) -> pz)
关于f# - 从 F# 中的一组元组中获取元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37178727/