haskell - Haskell 中的常量和模式匹配

标签 haskell pattern-matching pattern-synonyms

这个问题在这里已经有了答案:





Haskell - Using a constant in pattern matching

(2 个回答)


5年前关闭。




如何在 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 - Haskell 中的常量和模式匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35417305/

相关文章:

haskell - 最左-最内和最外 (Haskell)

rust - 是否可以解构方法的 `self` 参数?

haskell - 组合模式

haskell - 为多态模式同义词编写完整的编译指示?

haskell - 我怎样才能写出这个模式同义词而没有歧义的类型错误?

haskell - 为什么 *.prof 文件中有这么多反斜杠 (\)?

haskell - 什么是度量?

haskell - 在 Haskell 中从未知序列创建不同类型?

rust - 如何在字符串切片向量上进行模式匹配并提取值?

swift - 如何在 Swift 中使用模式匹配重载函数?