[Haskell-cafe] Parse do block problem

David Menendez dave at zednenem.com
Mon Dec 21 23:31:46 EST 2009


On Mon, Dec 21, 2009 at 11:14 PM, joeltt <joel at harpsoft.com> wrote:
>
> I'm trying to write my first Haskell program. The below is the first real
> logic block I've tried to write, unfortunately I get a "The last statement
> in a 'do' construct must be an expression" error when loading the method.
> However, the location of this problem isn't very clear. Is there a way to
> get more detailed parse message from Haskell, or can someone tell me where
> the problem is (and better "why"?). I don't think I actually need to use a
> "do" IO/Monad theme here, but its not clear to me either way. This isn't
> homework, its just for fun...
>
>
>    do_solve_iter guess tried = do
>                let actual = count_occurences guess
>    if guess == actual
>       then putStrLn "ANSWER!!"
>       else if (find (==actual) tried) == Just actual
>           then do
>               putStrLn "NO ANSWER!"
>               putStrLn tried
>           else do
>               putStrLn "ITER"
>               do_solve_iter actual (actual : tried)

Assuming your indentation didn't get lost in transmission, your
problem is the line "if guess == actual", which needs to be at the
same level of indentation as "let actual...". As written, the do-block
is terminating after the let-statement, which isn't permitted.

Also, unless there is more to the function, you don't really need the
outermost do-block at all. You can rewrite it easily as a let...in
expression, or move the definition of actual to a where clause and use
guards to avoid the nested if-expressions.


do_solve_iter guess tried
    | guess == actual = putStrLn "ANSWER!!"
    | find (==actual) tried == Just actual = do
        putStrLn "NO ANSWER"
        putStrLn tried
    | otherwise = do
        putStrLn "ITER"
        do_solve_iter actual (actual : tried)
    where
    actual = count_occurences guess

-- 
Dave Menendez <dave at zednenem.com>
<http://www.eyrie.org/~zednenem/>


More information about the Haskell-Cafe mailing list