操作员进行缩进 [英] Operator on do indentation

查看:141
本文介绍了操作员进行缩进的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hindent 将我的代码更改为:

  do download i inputFile 
onException
(callProcess(List.head args)(List.tail args))
(removeFileIfExists name)
`finally` removeFileIfExists inputFile

我无法确定 finally 是否适用于<$ c的其余部分$ c> do 块,或者仅仅是 onException 的状态。根据这个


如果您在列表中看到某些意想不到的情况,比如where,请插入
右大括号而不是分号。

我不确定这条规则是否适用于此。



`finally` 适用于其余的do,或只是最后一个语句,为什么?

解决方案

我们可以找出使用GHCi :写作

  Prelude>让f =(>>)
Prelude> :{
Prelude |打印5
Prelude |打印4
Prelude | `f`打印3
Prelude |打印2
Prelude | :}

会导致以下类型的错误(不是解析错误! b

 < interactive>:12:8:error:
•无法匹配预期类型'(() - &> IO()) - >整数 - > IO b'
与实际类型'IO()'
•函数'print'应用于三个参数
,但其类型'Integer - > IO()'只有一个
在'f'的第二个参数中,即'print 3 print 2'
在表达式中:
do {print 5;
print 4}
`f` print 3 print 2

我们发现GHCi是如何解析代码的,这些代码是用明确的大括号和分号打印出来的。



在那里,我们看到 f <部分关闭 do block!这使得整个 do 块成为 f 的第一个参数。此外,不再处于块中的下一行现在形成单个表达式 print 4 print 2 ,它用作 f的第二个参数。这会触发一个类型错误,因为它使用三个参数调用 print

确实,括号`f` 之前插入了> :当某个块不解析时,我们添加<$如果`f` } 并继续。 总结一下,缩进 more ,块被解析为

  do print 5 
print 4`f `print 3
print 2

如果`f` 缩进为前一行,或 less ,块被解析为

 (做{print 5 
; print 4})`f` print 3 print 2

我建议避免与上一行一样缩进`f` >:最好少一点缩进,以便解析变得明显即使是给读者也是如此。

hindent changed my code to:

do download i inputFile
   onException
     (callProcess (List.head args) (List.tail args))
     (removeFileIfExists name)
   `finally` removeFileIfExists inputFile

I can't determine if the finally applies to the rest of the do block, or just the state beginning onException. According to this,

If you see something unexpected in a list, like where, insert a closing brace before instead of a semicolon.

I'm unsure if that rule is applying here.

Does the `finally` apply to the rest of the do, or just the last statement and why?

解决方案

We can find out using GHCi: writing

Prelude> let f = (>>)
Prelude> :{
Prelude| do print 5
Prelude|    print 4
Prelude|    `f` print 3
Prelude|    print 2
Prelude| :}

causes the following type error (not a parse error!)

<interactive>:12:8: error:
    • Couldn't match expected type ‘(() -> IO ()) -> Integer -> IO b’
                  with actual type ‘IO ()’
    • The function ‘print’ is applied to three arguments,
      but its type ‘Integer -> IO ()’ has only one
      In the second argument of ‘f’, namely ‘print 3 print 2’
      In the expression:
        do { print 5;
             print 4 }
        `f` print 3 print 2

Looking at the list lines, we discover how GHCi parsed the code, which is printed with explicit braces-and-semicolons.

There, we see that the `f` part closed the do block! This makes the whole do block to be the first argument to f. Further, the next lines, no longer being in a block, now form a single expression print 4 print 2 which is used as the second argument for f. This triggers a type error since it calls print with three arguments.

Indeed, the brace } was inserted before `f` because of the rule mentioned by the OP: when something does not parse in a block, we add } and continue.

Summarizing, if `f` is indented more, the block is parsed as

do print 5
   print 4 `f` print 3
   print 2

If `f` is indented as the previous line, or less, the block is parsed as

(do { print 5
    ; print 4 }) `f` print 3 print 2

I would suggest to avoid indenting `f` exactly as the previous line: it is better to indent it less, so that the parsing becomes obvious even to a human reader.

这篇关于操作员进行缩进的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆