为什么使用我的活动模式对元组进行模式匹配不起作用?

Why doesn't pattern matching on a tuple using my active patterns work?

以下是我定义的活动模式以及尝试进行模式匹配的函数:

let (|Left|)     i = i = 0
let (|Top|)      j = j = 0
let (|Right|)  w i = i = w - 1
let (|Bottom|) h j = j = h - 1

let test w h (i, j) =
    match i, j with
    | (Left   , Top     ) -> 1
    | (Left   , Bottom h) -> 2
    | (Right w, Top     ) -> 3
    | (Right w, Bottom h) -> 4
    | (_      , _       ) -> 5

我预计这会匹配,例如,(0, 0)(Left, Top)(99, 99)(Right 100, Bottom 100)。相反,它甚至没有编译(其中一个错误是“FS0001:此表达式的类型应为 'bool',但此处的类型为 'unit'”)。

为此你需要 partial active patterns:

let (|Left|_|)     i = if i = 0 then Some Left else None
let (|Top|_|)      j = if j = 0 then Some Top else None
let (|Right|_|)  w i = if i = w - 1 then Some Right else None
let (|Bottom|_|) h j = if j = h - 1 then Some Bottom else None

let test w h (i, j) =
    match i, j with
    | (Left   , Top     ) -> 1
    | (Left   , Bottom h) -> 2
    | (Right w, Top     ) -> 3
    | (Right w, Bottom h) -> 4
    | (_      , _       ) -> 5

test 100 100 (0, 0)   |> printfn "%A"   // (Left, Top)     -> 1
test 100 100 (99, 99) |> printfn "%A"   // (Right, Bottom) -> 4