We are all used to pattern matching for cases when something is a particular type, like
match x with | Y(x) :: tail -> ... // assumes List.head(x) is of type Y(x)
But how can I compare the case when something is not of a certain type? For instance.
match x with | Not Y(_) :: tail -> ... // List.head(x) is definitely not a Y
Thank!
While there is no direct support Not, you can use a partial active template .
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]
Output
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"
, - , ... , , .
Source: https://habr.com/ru/post/1793888/More articles:how to easily run sinatra / padrino application on rails hostel - ruby | fooobar.comUsing ProGuard with reflection for startBluetoothSCO () in an Android application - androidобъект python AttributeError: type object 'Track' не имеет атрибута 'title' - pythonWhere is the living structure with the reference type as a property - garbage-collectionHow to draw using xml layout in android - androidC ++ library for converting from a Cartesian system to latitude, longitude and height - c ++Is it "tearing" MVC to get model information in a view? - asp.net-mvc-3How to convert Eastern time zone to CENTRAL TIME zone - timezoneNew classes added to the assembly were not found through reflection - reflectionПолучите элемент внутри iframe и присвойте ему значение с помощью java script - javascriptAll Articles