我遇到了棘手的缩进导致的问题,这里的代码是看起来在VI中:
1 import Data.List
2 myQuickSort [] = []
3 myQuickSort (x:xs) = myQuickSort smaller ++ [x] ++ myQuickSort bigger
4 where smaller = filter ( < x ) xs
5 bigger = filter ( >=x ) xs
但是在./cat 3.hs之后,看起来,
root@pierr-desktop:/opt/playGround/haskell# cat 3.hs
import Data.List
myQuickSort [] = []
myQuickSort (x:xs) = myQuickSort smaller ++ [x] ++ myQuickSort bigger
where smaller = filter ( < x ) xs
bigger = filter ( >=x ) xs
然后将其加载到ghci
GHCi, version 6.8.2: http://www.haskell.org/ghc/ :? for help
Loading package base ... linking ... done.
Prelude> :l 3.hs
[1 of 1] Compiling Main ( 3.hs, interpreted )
3.hs:5:11: parse error on input `='
Failed, modules loaded: none.
Prelude>
编程haskell时如何捕获这个不可见的缩进错误?
编辑: 这样写,错误就会出现。它是一种推荐的方式来编写where binding - 把变量放在不同的行中吗?
myQuickSort [] = []
myQuickSort (x:xs) = myQuickSort smaller ++ [x] ++ myQuickSort bigger
where
smaller = filter (<x) xs
bigger = filter (>=x) xs
答案 0 :(得分:6)
也许这是一个标签问题。制表符可能看起来与一定数量的空格相同,但Haskell不会认为它们是相同的
答案 1 :(得分:3)
您的问题与标签的扩展有关。 Haskell假设一个标签值8个空格。您的编辑可能有不同的假设。尝试在编辑器中搜索并替换8个空格的所有选项卡,然后调整间距以排列where子句。
答案 2 :(得分:0)
qs.hs:
import Data.List
myQuickSort [] = []
myQuickSort (x:xs) = myQuickSort smaller ++ [x] ++ myQuickSort bigger
where smaller = filter ( < x ) xs
bigger = filter ( >=x ) xs
GHCI:
GHCi, version 6.8.2: http://www.haskell.org/ghc/ :? for help Loading package base ... linking ... done. Prelude> :l qs.hs [1 of 1] Compiling Main ( qs.hs, interpreted ) Ok, modules loaded: Main. *Main> myQuickSort [9,8,7,6] [6,7,8,9]
您使用的是什么版本的GHCi?
你说(建议)你的程序只有那5行,但错误在第61行。而且,它在第14列,而第14列在你给出的那5行中没有=
例如,如果where在其自己的行上,或者如果它在上面的行上,该程序也可以为我加载。