Question about error message

Alastair Reid alastair@reid-consulting-uk.ltd.uk
20 Aug 2002 11:41:49 +0100


> Now, since no good deed goes unpunished, let me toss another
> question (or two) at you.  Here is some code that only works with
> the do construct, and I can't see why it is necessary.  This occurs
> regardless of whether anything else in the program uses the do
> construct, so it isn't one of those cases where something that is
> called is monadic and thus the caller needs to be monadic as well.
> The fragment is:

> displaySolutions (x:xs) = do 
>   displayOneSolution x 
>   displaySolutions xs

> Why is the "do" necessary for the second pattern?  If I leave it
> out, the compiler complains that I'm applying "displayOneSolution"
> to too many arguments, namely "x displaySolutions xs". 

Without the do, Haskell doesn't recognize it as an attempt to use
monadic syntax so it reads it as

> displaySolutions (x:xs) = displayOneSolution x displaySolutions xs

That is, displayOneSolution applied to 3 arguments.

> Parentheses don't help; then it just complains that I'm applying
> "(displayOneSolution x)" to too many arguments, namely
> "displaySolutions xs".  Other placements of parentheses change the
> error message but do not eliminate the problem.

The problem is that you have to use monadic bind instead of the
implicit apply.  If you don't use monad syntax, you have to write >>
or >>= explicitly:

> displaySolutions (x:xs) = displayOneSolution x >> displaySolutions xs

> One last question.  What is the syntax to use in an IO environment
> to do nothing?  For example, I have a function of type "IO ()", with
> some patterns, and I want to add another pattern that accepts an
> empty list and, essentially, does nothing with it.  Do I use "return
> ()"?

In _all monads_ the syntax is 'return ()'

--
Alastair Reid                 alastair@reid-consulting-uk.ltd.uk  
Reid Consulting (UK) Limited  http://www.reid-consulting-uk.ltd.uk/alastair/