Haskell:用Let语句缩进 [英] Haskell : Indentation with Let Statements
问题描述
恐怕我理解了这一点,并且通常 let
块被禁止。
以下代码:
splitWith tf xs
|(null xs)= [[]]
| ((长度xs)== 1)= [xs]
|否则= let tpl =(break tf xs)
x =(fst tpl)
xrst =(snd tpl)
在[[x]中,(splitWith tf(tail xrst))]
创建此语法错误:
[1 of 1]编译Main(ch04_exercise.hs,解释)
ch04_exercise.hs:31:26:解析错误输入'x'
在Sublime Text 2中摆脱了选项卡后,用于排列变量的空格。在我这样做之前,我在 x
后面的=号处得到了语法错误。
任何想法我做错了什么? 中的必须与<$ c $
| 在[[x],splitWith tf(tail xrst)]
$ b $中另有= let(x,xrst)= break tf xs
b
这就是为什么我通常更喜欢把 let
放在一行上的原因:
|在[[x],splitWith tf(tail xrst)]
> 您也有不必要的括号,并且可以从一些模式匹配中获益(如上所示)。
I'm afraid I'm tapped out on understanding this and the let
block in general.
The following code:
splitWith tf xs
|(null xs) = [[]]
|((length xs) == 1) = [xs]
|otherwise = let tpl = (break tf xs)
x = (fst tpl)
xrst = (snd tpl)
in [[x] ,(splitWith tf (tail xrst))]
Creates this syntax error:
[1 of 1] Compiling Main ( ch04_exercise.hs, interpreted )
ch04_exercise.hs:31:26: parse error on input ‘x’
This happened after I got rid of tabs in Sublime Text 2 and put spaces in to line up the variables. Before I did that I got the syntax error at the = sign after the x
.
Any idea what I'm doing wrong?
The in
has to line up with the let
or be on the same line:
| otherwise = let (x, xrst) = break tf xs
in [[x], splitWith tf (tail xrst)]
Which is why I usually prefer putting let
on its own line:
| otherwise =
let (x, xrst) = break tf xs
in [[x], splitWith tf (tail xrst)]
You also have unnecessary parentheses and could benefit from some pattern matching (as demonstrated above).
这篇关于Haskell:用Let语句缩进的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!