haskell中的缩进规则的完整集 [英] Complete set of Indentation rules in haskell

查看:251
本文介绍了haskell中的缩进规则的完整集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从哪里可以得到一套完整的缩进规则Haskell代码写作?



过去的SO问题类似于我的以下问题导致我问上面的问题。错误消息背后的原因是:输入something时解析错误




baby.hs:103:2:输入「myList」时发生解析错误b

代码我想编译:

  myList = 飞机,飞机,飞机,直升机,直升机,斑马] 
quicksort ::(Ord a)=> [a] - > [a]
quicksort [] = []
quicksort(x:xs)=
let smallerSorted = quicksort [a | a< - xs,a< = x]
greaterSorted = quicksort [a | a< - xs,a> x]
in smallerSorted ++ [x] ++ greaterSorted

/ p>

我将问题中的代码转移到新的.hs文件,并试图编译它。仍然有类似的错误消息。详情如下:


错误

quickSort.hs:5:62:输入时解析错误`='
失败,模块加载:无。
(0.02 secs,0 bytes)



代码
quicksort ::(Ord a)=> [a] - > [a] >
quicksort [] = []
quicksort(x:xs)=
let smallerSorted = quicksort [a | a< - xs,a< = x]

greaterSorted = quicksort [a | a x]

在smallerSorted ++ [x] ++ greaterSorted

myList = [飞机,飞机,飞机直升机,HELICOPTER,Zebra,美国]



解决方案

对于标准Haskell(无语言扩展),布局规则在Haskell 2010年报告的第10.3节中介绍了



报告可能对您的口味有点正式。 Haskell上的各种教程和教科书对布局规则有一些更正式的解释,例如 Haskell Wikibook 。 p>

在特定情况下,错误消息指向第103行,但您只粘贴了几行。我可以看到,至少let-in结构的缩进被打破。你应该写如下:

  xs = [飞机,飞机,飞机,直升机 ,HELICOPTER,Zebra] 
quicksort ::(Ord a)=> [a] - > [a]
quicksort [] = []
quicksort(x:xs)= let smallerSorted = quicksort [a | a< - xs,a< = x]
greaterSorted = quicksort [a | a< - xs,a> x]
in smallerSorted ++ [x] ++ biggerSorted


From where can I get complete set of Indentation rules for Haskell code writing?

Past SO questions which are similar to my following question has led me to ask above question. What are the reasons behind the error message: parse error on input 'something'?

Error message I got:

baby.hs:103:2: parse error on input `myList'(error in this line)

Code I am trying to compile:

myList = ["aeroplane", "Aeroplane", "AeRoPlAne", "helicopter", "HELICOPTER", "Zebra"]
quicksort :: (Ord a) => [a] -> [a]  
quicksort [] = []  
quicksort (x:xs) =
let smallerSorted = quicksort [a | a <- xs, a <= x]  
biggerSorted = quicksort [a | a <- xs, a > x]  
in smallerSorted ++ [x] ++ biggerSorted

Edit by Optimight:

I shifted the code in the question to new .hs file and tried to compile it. Still similar error message remains. Details below:

Error
quickSort.hs:5:62: parse error on input `=' Failed, modules loaded: none. (0.02 secs, 0 bytes)

Code quicksort :: (Ord a) => [a] -> [a]
quicksort [] = [] quicksort (x:xs) = let smallerSorted = quicksort [a | a <- xs, a <= x]
biggerSorted = quicksort [a | a <- xs, a > x]
in smallerSorted ++ [x] ++ biggerSorted
myList = ["aeroplane", "Aeroplane", "AeRoPlAne", "helicopter", "HELICOPTER", "Zebra", "America"]

解决方案

At least for standard Haskell (without language extensions), the layout rules are explained in Section 10.3 of the Haskell 2010 report.

The report may be a bit formal for your taste. Various tutorials and textbooks on Haskell have some more informal explanations of the layout rules, for example the Haskell Wikibook.

In your particular case, the error message points to line 103, but you are only pasting a couple of lines. I can see that at least the indentation of the let-in construct is broken. You should write something like the following:

xs = ["aeroplane", "Aeroplane", "AeRoPlAne", "helicopter", "HELICOPTER", "Zebra"]
quicksort :: (Ord a) => [a] -> [a]
quicksort [] = []
quicksort (x:xs) = let smallerSorted = quicksort [a | a <- xs, a <= x]
                       biggerSorted = quicksort [a | a <- xs, a > x]
                   in smallerSorted ++ [x] ++ biggerSorted

这篇关于haskell中的缩进规则的完整集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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