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
When you do
match cell1.X, cell2.X with
| cell1, cell2
you have created a new cell1
variable, which is cell1.X
(an int).
I would probably have just used an if here, or change to | _,_ when ...
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...