错误 - 输入中的语法错误(意外符号"大")

时间:2015-04-01 09:45:18

标签: haskell

我写了这段代码,它是关于两个字符串是否是另一个字符串:

anagram :: String->String->Bool            
anagram w1 w2 = anagram1 x1 x2
where 
    y1 = break w1
    y2 = break w2
    x1 = quicksort y1
    x2 = quicksort y2


anagram1 :: [String]->[String]->Bool
anagram1 (h1:t1)(h2:t2)
    | h1!=h2 = False
    | h1==h2 = anagram1 t1 t2
    | otherwise = True  

我找到了快速排序的代码http://c2.com/cgi/wiki?QuickSortInHaskell

quicksort :: [String]->[String]
quicksort [] = []
quicksort (h:t) = quicksort small ++ (h : quicksort(large))
   where
      small = [y | y <- t, y <= h]
      large = [y | y <- t, y > h]


break :: String->[String]
break s = map (\c -> [c]) s

当我运行它时,我在输入中输入错误语法错误(意外符号&#34;大&#34;)

为什么我错了?

1 个答案:

答案 0 :(得分:5)

如果我将它加载到ghci中它会完美运行。

也许你的缩进在where子句中是不正确的。

anagram中,where关键字必须缩进,而在haskell中,&#34;不等于&#34;是/=不是 !=

请使用空格到处

YourModule.break必须为anagram,因此不会与Prelude.break冲突。

只需阅读错误消息,计算机正试图与您通话,所以请听;)

在GHCi中测试大量代码,您将更加熟悉Haskell