操作员进行缩进 [英] Operator on do indentation
问题描述
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是如何解析代码的,这些代码是用明确的大括号和分号打印出来的。
在那里,我们看到 确实,括号 如果 我建议避免与上一行一样缩进 I can't determine if the 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 We can find out using GHCi: writing causes the following type error (not a parse error!) 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 Indeed, the brace Summarizing, if If I would suggest to avoid indenting 这篇关于操作员进行缩进的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! f <
部分关闭 do
块成为 f
的第一个参数。此外,不再处于块中的下一行现在形成单个表达式 print 4 print 2
,它用作 f的第二个参数
。这会触发一个类型错误,因为它使用三个参数调用 print
。
`f`
之前插入了> :当某个块不解析时,我们添加<$如果`f`
是
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
finally
applies to the rest of the do
block, or just the state beginning onException
. According to this,
`finally`
apply to the rest of the do, or just the last statement and why?Prelude> let f = (>>)
Prelude> :{
Prelude| do print 5
Prelude| print 4
Prelude| `f` print 3
Prelude| print 2
Prelude| :}
<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
`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.}
was inserted before `f`
because of the rule mentioned by the OP: when something does not parse in a block, we add }
and continue.`f`
is indented more, the block is parsed asdo print 5
print 4 `f` print 3
print 2
`f`
is indented as the previous line, or less, the block is parsed as(do { print 5
; print 4 }) `f` print 3 print 2
`f`
exactly as the previous line: it is better to indent it less, so that the parsing becomes obvious even to a human reader.