考虑以下数据类型和模式同义词:
{-# LANGUAGE PatternSynonyms, NamedFieldPuns #-}
data Foo = Foo {
a :: Int
, b :: String
, c :: Maybe Bool
}
pattern Bar a b <- Foo { a, b }
pattern Baz c <- Foo { c }
Run Code Online (Sandbox Code Playgroud)
我想匹配Foo,但得到所有的a,b和c.像这样的东西(无效的Haskell):
showit :: Foo -> String
showit (Bar a b & Baz c) = show a ++ b ++ show c
Run Code Online (Sandbox Code Playgroud)
一种选择是使用ViewPattern:
dup :: a -> (a, a) …Run Code Online (Sandbox Code Playgroud)