use*_*018 3 haskell pattern-matching pattern-synonyms
如何在Haskell中定义宏常量?特别是,我想在没有第二个模式匹配的情况下运行以下代码片段重叠.
someconstant :: Int
someconstant = 3
f :: Int -> IO ()
f someconstant = putStrLn "Arg is 3"
f _ = putStrLn "Arg is not 3"
Run Code Online (Sandbox Code Playgroud)
lef*_*out 12
您可以定义模式同义词:
{-# LANGUAGE PatternSynonyms #-}
pattern SomeConstant :: Int
pattern SomeConstant = 3
f :: Int -> IO ()
f SomeConstant = putStrLn "Arg is 3"
f _ = putStrLn "Arg is not 3"
Run Code Online (Sandbox Code Playgroud)
但也要考虑在自定义变体类型上匹配是否更好Int.