函数式编程之-模式匹配(Pattern matching) (2)

when语句尽管可以给模式添加一些条件,但是当语句过于复杂的时候可以考虑某个分支的模式定义为一个方法:

open System.Text.RegularExpressions // create an active pattern to match an email address let (|EmailAddress|_|) input = let m = Regex.Match(input,@".+@.+") if (m.Success) then Some input else None // use the active pattern in the match let classifyString aString = match aString with | EmailAddress x -> printfn "%s is an email" x // otherwise leave alone | _ -> printfn "%s is something else" aString //test classifyString "alice@example.com" classifyString "google.com"

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zywxzw.html