[email protected]
[Top] [All Lists]

Re: [Haskell-cafe] Parse do block problem

Subject: Re: [Haskell-cafe] Parse do block problem
From: David Menendez
Date: Mon, 21 Dec 2009 23:31:46 -0500
On Mon, Dec 21, 2009 at 11:14 PM, joeltt <[email protected]> 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)
    actual = count_occurences guess

Dave Menendez <[email protected]>
Haskell-Cafe mailing list
[email protected]

<Prev in Thread] Current Thread [Next in Thread>