Haskell:我的缩进错误是什么

时间:2011-01-04 00:26:01

标签: haskell indentation

我通过以下代码获得了我认为的缩进错误

152     -- find the first one who can refute the scenario
153     let cs = map ($scenario) [ Suspect . getWho, Room . getWhere, Weapon . getHow ]
154     let (qs,rss) = break (not . null . intersect cs . hand) ps
155     unless (null rss) $ do
156       let refuter:ss= rss
157       let valid = intersect cs $ hand refuter
158 
159       (refuter, shown) <- if cheated refuter
160                             -- if the refuter is a cheater, just choose for him
161                             then return (refuter, head valid)
162                             else do
163                               (refuter, shown) <- runStateT $ reveal scenario
164                               -- make sure they don't cheat
165                               return if shown `elem` valid
166                                         then (refuter, shown)
167                                         else (refuter { cheated = True }, head valid)
168       putTail (qs ++ refuter:ss)
169     return ()

我得到的错误是

Game.hs:159:6:
    The last statement in a 'do' construct must be an expression

当我删除或注释掉第159-167行时,错误就会消失。

我做错了什么?

2 个答案:

答案 0 :(得分:5)

你需要在第165行的return值的if-then-else周围添加括号:

return (if shown `elem` valid
           then (refuter, shown)
           else (refuter { cheated = True }, head valid))

Haskell语法作为函数参数的纯if-then-else不是allowed,它必须放在括号中以便在那个地方使用。 (它是 lexp ,而函数参数必须是 aexp 。)

生成的错误消息对查找该问题没有多大帮助。

答案 1 :(得分:4)

return $ if ..

这应该有效。功能应用优先级问题。