haskell - 为什么在没有额外缩进的情况下换行大小写匹配会出现语法错误,以及推荐的解决方法是什么?

标签 haskell syntax switch-statement indentation

我才发现这个

foo = case ((), ()) of
       ( ()
       , () ) -> ()

失败
/tmp/wtmpf-file11080.hs:3:8:
    parse error (possibly incorrect indentation or mismatched brackets)

这可以通过缩进模式的第二行来实现
foo = case ((), ()) of
       ( ()
        , () ) -> ()

但这感觉与我平时的风格不一致,尤其是在
bar = case ( some lengthy :: Complicated typed expression
           , another also lengthy :: Expression with (Other types) ) of
       ( Complicated (Pattern match) to (unwrap)
       , Expression that's (Again not so short) ) -> the Rest of my Code

应如何重写/格式化上述内容以使其看起来最一致?

最佳答案

indentation rules , 编码

foo = case ((), ()) of
       ( ()
       , () ) -> ()

脱糖为
foo = case ((), ()) of
       { ( ()
       ; , () ) -> ()
       }

这是一个 case有两个分支,第一个是语法错误。

我会推荐以下样式:
foo = case ((), ()) of
       (( ()
        , () )) -> ()

甚至(虽然不是特别优雅)
foo = case ((), ()) of
       _@( ()
         , () ) -> ()

关于haskell - 为什么在没有额外缩进的情况下换行大小写匹配会出现语法错误,以及推荐的解决方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40934464/

相关文章:

python - python函数如何真正改变参数而不是形式参数?

Javascript范围变量切换大小写?

java - If else vs switch with stack 逻辑

haskell - Haskell 是否提供了与许多可能的数据构造函数进行模式匹配的习语?

haskell - 无点组合器中的模式,与 SKI 演算的关系

Python:for语句行为

java - && 语法错误 AGP 新手指南

C# - Is 语句在 Switch 语句中

haskell - 为什么我必须按字段强制此数据类型,而不是一次全部强制?

haskell - 在单子(monad)变压器类型类中使用列表单子(monad)?