从哪里可以获得Haskell代码编写的完整缩进规则集?
过去的SO问题与我的以下问题类似,这让我提出上述问题。错误消息背后的原因是什么:parse error on input 'something'
?
我收到错误消息:
baby.hs:103:2:输入`myList'时解析错误(此行中的错误)
我正在尝试编译代码:
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
通过Optimight编辑:
我将问题中的代码转移到新的.hs文件并尝试编译它。仍然存在类似的错误消息。详情如下:
错误
quickSort.hs:5:62:输入`='解析错误 失败,模块加载:无。 (0.02秒,0字节)代码 quicksort ::(Ord a)=&gt; [a] - &gt;并[a]
quicksort [] = [] quicksort(x:xs)= 让smallSorted = quicksort [a | a&lt; -xs,a&lt; = x]
greaterSorted = quicksort [a | a&lt; -xs,a&gt; X]
in smallSorted ++ [x] ++ greaterSorted
myList = [“飞机”,“飞机”,“飞机”,“直升机”,“直升机”,“斑马”,“美国”]
答案 0 :(得分:12)
至少对于标准Haskell(没有语言扩展),解释了布局规则in Section 10.3 of the Haskell 2010 report.
根据您的口味,报告可能有点正式。关于Haskell的各种教程和教科书对布局规则有一些非正式的解释,例如Haskell Wikibook.
在您的特定情况下,错误消息指向第103行,但您只粘贴了几行。我可以看到,至少let-in结构的缩进被打破了。你应该写下面的内容:
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
答案 1 :(得分:2)
如果我理解你,最简单的方法是避免这个问题,就像这样编写单do
和let
:
fName = do
expression1 = ...
exp2 ...
fName param = let
exp1 = ...
exp2 = ...
exp3 = ...
in ... -- can be indented more if you want
这里的要点是exp1
的第一个缩进很容易在每个新行中找到并保持。
这是我的学习单上的复制/粘贴。它并不多,但可能对某人有帮助。 如果有人发现任何错误/错误,请纠正我。
制表符或空格表示BLOCK,如{com1; com +}在C ++中
越位规则:在源文件的开头,第一个顶级 声明或定义可以从任何列开始。随后每一次 顶级声明必须有相同的缩进!
让我们回想起它看到的下一个标记的缩进。如果是空的 线,或缩进是向右,它继续前一行。 如果是相同的缩进,则这是同一块中新项目的开头。
win tab 4 spc,unix tab 8 spaces使用空格代替标签!!!
line1...
continue line 1 -- as long it doesn't start at same indent as line1
3spaces would indicate block with line 1!
line4 same block
same indent is same line1 block
all indents for BLOCK must be same.
line8... -- is new block and end of line1 block
positon of first column in block is start
line 1
cont line1
cont line 1
cont line 1
block1 -- would be cont line 1
block1 -- if not for this block1 at same indent
Bad way:
do
action0
if condition
then action1
else action2
action3
Good way:
do
action0
if condition
then action1
else action2
action3