I have an haskell problem. putStrLn is supposed to take a [Char], or a String, and even though it seems like I give that to mr Compiler, he still complains.
*** Expression : putStrLn line
*** Term : line
*** Type : Char
*** Does not match : [Char]
The code that it refers to is this:
getV::[[(Char,Float)]] -> IO ()
getV [] = putStrLn ""
getV (x:xs) = do line <- getS x
putStrLn line <-- Complaining line
getV xs
getS::[(Char,Float)] -> String
getS [] = ""
getS ((c,f):str) = do a <- "(L" ++ [c] ++")" ++ getS str
return a
I did strip it a little bit, but it should be exactly the same behaviour. getS do return a String and that string is the argument for putStrLn. So what is the problem? :/
Since your
getSreturns aString, not anIO String, there’s no need to “extract” the pure value with<-.Just use
Also, there’s a
mapMfunction which you canand your
getSis using monads unnecessarily.Of course, it’s possible to write it using built-in functions only.
The reason your code doesn’t fail on the
line <- getS xline, andlinebecomes aCharis because List is also a monad. For instance, we can write the Cartesian product asIn fact, list comprehension is based on this monadic property of list.