Why doesn't pattern matching on a property of a record compile?

后端 未结 2 1618
借酒劲吻你
借酒劲吻你 2021-01-27 23:49

Why doesn\'t pattern matching on a property of a record compile?

type Cell = { X:int; Y:int }

let isNeighbor cell1 cell2 =
    match cell1.X, c         


        
2条回答
  •  星月不相逢
    2021-01-28 00:29

    As John Palmer already explained, you are shadowing the original parameters. If pattern matching does not feel well suited if-elif-else might be better. If you want to use pattern matching, the following could be helpful:

    let isNeighbor { X=x1; Y=y1 } { X=x2; Y=y2} =
        match abs(x1-x2), abs(y1-y2) with
        | 0,1 | 1,0 | 1,1 -> true
        | _ -> false
    

    Remove the 1,1 pattern if diagonal adjacent cells should not be neighbors. And then, there is also Euclidean distance...

提交回复
热议问题