小编wrl*_*wrl的帖子

结合模式

考虑以下数据类型和模式同义词:

{-# 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,bc.像这样的东西(无效的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)

haskell pattern-synonyms

10
推荐指数
1
解决办法
211
查看次数

标签 统计

haskell ×1

pattern-synonyms ×1