简体   繁体   English

解析案例表达式时出错

[英]Error parsing case expression

I am getting this compile error: 我收到此编译错误:

Files.hs:47:17: parse error on input ‘->’
Failed, modules loaded: none.

In the following section of code: 在以下代码部分中:

main :: IO ()
main = do
    args <- getArgs
    let f = case args of
        ("W":_) -> eoltoW
        --      ^  here's 47:17
        ("U":_) -> eoltoU
        _       -> fail "2 - 3 arguments required"
    case args of
        [_,i,o] -> editWith f i o
        [_,i]   -> catWith  f i
        [_]     -> fail "2 - 3 arguments required"

While I understand the logic could use some tidying up, I do not see where I am going wrong with case syntax. 虽然我知道逻辑可能需要进行一些整理,但我看不出大小写语法出了什么问题。 I figure it might be some weird interaction with do and let , but I can't find any clue as to how to correct it. 我认为与dolet可能是一些奇怪的交互,但是我找不到关于如何更正它的任何线索。

Note, I have ensured that I only use spaces for indentation 注意,我确保只使用空格进行缩进

Edit: 编辑:

It seems that adding a single space further of indentation (as below) is sufficient to prevent the error, but I am unclear as to why. 似乎在缩进处再加上一个空格(如下所示)足以防止该错误,但是我不清楚原因。

main = do
    args <- getArgs
    let f = case args of
         ("W":_) -> eoltoW
         --      ^  here's 47:17
         ("U":_) -> eoltoU
         _       -> fail "2 - 3 arguments required"

This is described in 2.7 and 10.3 . 2.710.3中对此进行了描述。 Basically, the rule for let … in a do block* is that all bindings have to be indented the same way: 基本上,在do block *中let …的规则是,所有绑定必须以相同的方式缩进:

let a = …
    b = …
    c = …

Furthermore, the "…" have to be more indented than the layout list. 此外,“…”必须比布局列表缩进更多 For example, the following is a syntax error: 例如,以下是语法错误:

let a =
    10
in a

In order to create a new layout-list, we need to indent it further (see note 1 in section 10.3), which is why 为了创建一个新的布局列表,我们需要进一步缩进(请参阅第10.3节中的注释1),这就是为什么

let a =
     10
in a

is completely fine. 完全可以 The same holds for your case . 您的case也是case All of the cases have to be further indented than f due to the off-side rule. 由于越位规则,所有情况都必须比f缩进。

* that rule actually holds** for more, ie for let , where , do and of . *该规则实际上适用于**,例如, letwheredoof
** well, as long as you don't introduce additional braces **好吧,只要您不引入其他括号即可

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM