当某些东西不属于特定类型时如何进行模式匹配

我们都习惯于在某些特定类型的情况下进行模式匹配,例如,
match x with
| Y(x) :: tail -> ... // assumes List.head(x) is of type Y(x)
但是,当某些东西不属于特定类型时,我该如何匹配?例如。,
match x with
| Not Y(_) :: tail -> ... // List.head(x) is definitely not a Y
谢谢!     
已邀请:
虽然没有直接支持
Not
,但您可以使用部分活动模式。
type Color = | Red | Green | Blue

let (|NotRed|_|) = function
    | Red -> None
    | color -> Some color

let rec print = function
    | (NotRed head) :: tail -> 
        printfn "%A is Not Red" head
        print tail
    | _ :: tail -> print tail
    | _ -> ()

print [Red; Green; Blue]
产量
Green is Not Red
Blue is Not Red
    
我认为解决这个问题的通常方法是先写一个明确排除你不想要的案例的条款。然后你可以使用
_
来处理所有剩余的情况(你需要为你想要排除的情况编写一些代码,但是无论如何都需要编写代码来完成模式匹配):
match x with
| Y _ :: tail -> ()
| _ :: tail -> // List.head(x) is definitely not a Y
这绝对是一种解决方法,但我担心这是你能做的最好的事情。如果要排除多个案例,可以编写如下内容:
match x with
| (Y _ | (Z (1 | 2 | 3 | 4)) :: tail -> ()
| _ :: tail -> // Excludes all "Y x" and "Z n when n in 1,2,3,4"
无论如何,这是一个非常有趣的问题 - 我想知道模式的语言是否可以用一些特殊模式来扩展来表达否定...有趣的是,这不是可以使用活动模式直接编写的东西。     

要回复问题请先登录注册