我是haskell的初学编码员,同时从这本惊人的书的第一章开始练习:http://book.realworldhaskell.org/read/getting-started.html 我遇到了这个问题:
-- test comment
main = interact wordCount
where
wordCount input = show (ls ++ " " ++ ws ++ " " ++ cs ++ "\n")
where
ls = lines input
ws = length words input
cs = length input
wonderbox:ch01 manasapte$ runghc WC < quux.txt
WC.hs:5:9: parse error on input ‘where’
为什么我不能窝里?
答案 0 :(得分:10)
由于您的第二个where
附加到wordCount
定义,因此需要缩进比它更多。 (虽然之后你还会有其他一些错误。)
答案 1 :(得分:5)
其他人已经回答了。我只想补充一些解释。
简化一点,Haskell缩进规则是:
where
,let
,do
,case ... of
)。因此,
where
wordCount input = show (ls ++ " " ++ ws ++ " " ++ cs ++ "\n")
where
ls = lines input
ws = length words input
cs = length input
实际意味着
where {
wordCount input = show (ls ++ " " ++ ws ++ " " ++ cs ++ "\n")
;
where { -- same column, new entry
ls = lines input
; -- same column, new entry
ws = length words input
; -- same column, new entry
cs = length input
}
}
将第二个where
视为与wordCount
无关的单独定义。如果我们更多地缩进它:
where {
wordCount input = show (ls ++ " " ++ ws ++ " " ++ cs ++ "\n")
where { -- after the pivot, same entry
ls = lines input
;
ws = length words input
;
cs = length input
}
}
答案 2 :(得分:2)
缩进不正确,这是工作版本:
-- test comment
import Data.List
main = interact wordCount
where wordCount input = unlines $ [concat $ intersperse " " (map show [ls, ws, cs])]
where ls = length $ lines input
ws = length $ words input
cs = length input