Haskell 在let语句中使用where子句时出错

Haskell 在let语句中使用where子句时出错,haskell,Haskell,这是我的密码: main = do contents <- getContents let threes = groupsOf 3 (map read $ lines contents) where groupsOf 0 _ = [] groupsOf _ [] = [] groupsOf n xs = take n xs : groupsOf n (drop n xs) putStrLn $ show th

这是我的密码:

main = do
   contents <- getContents 
   let threes = groupsOf 3 (map read $ lines contents)
      where groupsOf 0 _ = []
            groupsOf _ [] = []
            groupsOf n xs = take n xs : groupsOf n (drop n xs)
    putStrLn $ show threes

不知道我做错了什么。据我所知,我的语法是正确的…

您提出了许多语法问题

contents = <- getContents
如果没有进一步缩进,let子句后面不能有
where
。您可以在let子句中将
where
子句移动到函数后面,在let子句中声明
groupsOf
,或者将
where
缩进let子句中变量缩进一点:

let threes = groupsOf 3 (map read $ lines contents)
    groupsOf 0 _ = []
    groupsOf _ [] = []
    groupsOf n xs = take n xs : groupsOf n (drop n xs)

编辑:在参考了Haskell 2010报告之后,我认为,
let{decls},where{decls}
实际上不是有效的Haskell。GHC解析表达式,我认为这在某些情况下是合法的,而且很好,尽管这里的风格不好。

您提出了很多语法问题

contents = <- getContents
如果没有进一步缩进,let子句后面不能有
where
。您可以在let子句中将
where
子句移动到函数后面,在let子句中声明
groupsOf
,或者将
where
缩进let子句中变量缩进一点:

let threes = groupsOf 3 (map read $ lines contents)
    groupsOf 0 _ = []
    groupsOf _ [] = []
    groupsOf n xs = take n xs : groupsOf n (drop n xs)
编辑:在参考了Haskell 2010报告之后,我认为,
let{decls},where{decls}
实际上不是有效的Haskell。GHC解析表达式,我认为这在某些情况下是合法的,而且很好,尽管这里的样式不好。

可能重复的可能重复的