Haskell:用Let语句缩进 [英] Haskell : Indentation with Let Statements

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

问题描述

恐怕我理解了这一点,并且通常 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屋!

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