3

如何在 Haskell 中定义宏常量?特别是,我希望在不重叠第二个模式匹配的情况下运行以下代码段。

someconstant :: Int
someconstant = 3

f :: Int -> IO ()
f someconstant = putStrLn "Arg is 3"
f _            = putStrLn "Arg is not 3"
4

1 回答 1

13

您可以定义模式同义词

{-# LANGUAGE PatternSynonyms #-}

pattern SomeConstant :: Int
pattern SomeConstant = 3

f :: Int -> IO ()
f SomeConstant = putStrLn "Arg is 3"
f _            = putStrLn "Arg is not 3"

但还要考虑匹配自定义变体类型而不是Int.

于 2016-02-15T19:20:58.743 回答