Haskell 中的常量和模式匹配
Constants in Haskell and pattern matching
如何在Haskell中定义宏常量?特别是,我希望以下代码段 运行 没有第二个模式匹配被重叠。
someconstant :: Int
someconstant = 3
f :: Int -> IO ()
f someconstant = putStrLn "Arg is 3"
f _ = putStrLn "Arg is not 3"
你可以定义一个pattern synonym:
{-# LANGUAGE PatternSynonyms #-}
pattern SomeConstant :: Int
pattern SomeConstant = 3
f :: Int -> IO ()
f SomeConstant = putStrLn "Arg is 3"
f _ = putStrLn "Arg is not 3"
但也要考虑匹配自定义变体类型而不是 Int
是否更好。
如何在Haskell中定义宏常量?特别是,我希望以下代码段 运行 没有第二个模式匹配被重叠。
someconstant :: Int
someconstant = 3
f :: Int -> IO ()
f someconstant = putStrLn "Arg is 3"
f _ = putStrLn "Arg is not 3"
你可以定义一个pattern synonym:
{-# LANGUAGE PatternSynonyms #-}
pattern SomeConstant :: Int
pattern SomeConstant = 3
f :: Int -> IO ()
f SomeConstant = putStrLn "Arg is 3"
f _ = putStrLn "Arg is not 3"
但也要考虑匹配自定义变体类型而不是 Int
是否更好。