经典 Haskell 不提供通用模式匹配。它确实提供了标准模式匹配和保护。所以你可以写像
foo :: [Int] -> ...
foo [1,2,3] = <some expression>
foo [1,2,x] = <some expression that can use x>
foo (x:xs) = <some expression where x is the first element, and xs is the rest>
foo (x:x':xs) = <some expression where x is the first element, x' the second, and xs is the rest>
bar :: (Int,String) -> ...
bar (1,"hello") =
bar (someInt,someString) =
bar (someInt,_) =
bar _ =
交替:
bar :: (Int, String) -> ...
bar x = case x of
(1,"hello") -> ...
_ -> ...
交替:
bar :: (Int, String) -> ...
bar (someInt,someString)
| someInt == 1 && someString == "hello" = ...
| someInt == 2 && someString == "hello" = ...
| otherwise = ...
GHC 还提供扩展以更无缝地集成防护和模式匹配。请参阅“查看模式”和“模式守卫”部分:http ://www.haskell.org/ghc/docs/latest/html/users_guide/syntax-extns.html