[Haskell] Re: Going nuts

Peter Davis pediddle at pediddle.net
Wed Apr 20 22:51:30 EDT 2005

On 2005-04-20 19:04:32 -0700, "Alexandre Weffort Thenorio" 
<alethenorio at home.se> said:

> As usual a beginner in Haskell. Trying to write a simple program in haskel
> shown below
> [snip]
> getLeft :: String -> String -> String
> getRight :: String ->String -> String
> outputLine keyno key orgFile = do
>     part1 <- getLeft keyno orgFile
>     part2 <- getRight keyno orgFile
>     total <- part1 ++ (strUpper key) ++ part2 ++ "\n"
> [snip]
> And I keep getting the error
> changecode.hs:42:
>     Couldn't match `[a]' against `Char'
>         Expected type: [a]
>         Inferred type: Char
>     In the first argument of `(++)', namely `part1'
>     In a 'do' expression:
>         total <- part1 ++ ((strUpper key) ++ (part2 ++ "\n"))

You should be using:

  let part1 = getLeft keyno orgFile
  let part2 = getRight keyno orgFile
  let total = part1 ++ (strUpper key) ++ part2 ++ "\n"

The problem is that the "part1 <- ..." syntax is for extracting the 
result from a monadic computation.  When you read from a file like 
"hexFile <- readFile "file"", readFile is a computation in the IO 
monad, and you extract hexFile from the monad.  The list [] type is 
also a monad, and String is really [Char], so "part1 <- getLeft keyno 
orgFile" implies that part1 is of type Char, which is a single element 
extracted from the list of Chars returned by the monadic computation 
(in the [] monad) "getLeft keyno orgFile".

That leads to the error you see.  part1's inferred type is Char, and 
the ++ function expects a list of some type ([a]), which Char is 
obviously not.

The "let" syntax binds a variable instead of extracting it from a 
monadic computation, which is what you want for these three lines.

Hope that helps!

Peter Davis <pediddle at pediddle.net>
"Furthermore, I believe bacon prevents hair loss!"

More information about the Haskell mailing list