On Tue, 23 Nov 1999, Matthias Riese wrote:

> Hi!
> 
> ghc 4.04 refuses to compile this piece of code:
> 
> g :: [String] -> String
> g a = a!!0
> 
> f :: String
> f = do
>  x <- [""]
>  return (g x)
> 
> error message:
>     Couldn't match `Char' against `String'
>       Expected type: Char
>       Inferred type: String
>     In the application `g x'
>     In the first argument of `return', namely `(g x)'

The correct version is:

f :: Monad m => m String
f = do
  let x = [""]
  return (g x)

The <- in do notation extracts the value from the List monad, that is:

  do x <- [ "" ]

is equivalent to

  let x = ""

The type of f can't be String since the return expression encapsulates the
value in a monad.

Hope that helps

/Stefan

- stefan larsson -- http://www.dtek.chalmers.se/~d95stela -- ...och h�r sen! -
  
Here come the Martian Martians, and they're riding on their Martian bikes
and we have to find out right now, what kind of ice cream do the Martians like?

Reply via email to