Haskell: What's my indent error
I'm getting what I think is an indent error with the following code
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 ()
The error I'm getting is
Game.hs:159:6:
The last statement in a 'do' construct must be an expression
When I delete or comment out lines 159-167, the error goes away.
What am I doing wrong?
You need to put parenthesis around the if-then-else in the value for return
in line 165:
return (if shown `elem` valid
then (refuter, shown)
else (refuter { cheated = True }, head valid))
A pure if-then-else is not allowed by the Haskell grammar as a function argument, it has to be put in parenthesis to be used in that place. (It is a lexp while a function argument has to be an aexp.)
The error message generated is not very helpful in finding that issue.
return $ if ..
This should work. Matter of function application priority.
精彩评论