I try to learn Haskell and struggle with converting do-syntax into monads.
So, I boiled it down to a tiny example.
I got this:
work :: String
work = do
let arr = ["Get", "this", "to", "work"]
let s = unlines arr
s
main :: IO ()
main = putStrLn work
And I rephrase it to:
work :: String
work = ["Get", "this", "to", "work"] >>= unlines >>= return
main :: IO ()
main = putStrLn work
Which gave me:
monad-do.hs:2:42: error:
• Couldn't match type ‘Char’ with ‘[Char]’
Expected type: [Char] -> [Char]
Actual type: [String] -> String
• In the second argument of ‘(>>=)’, namely ‘unlines’
In the first argument of ‘(>>=)’, namely
‘["Get", "this", "to", "work"] >>= unlines’
In the expression:
["Get", "this", "to", "work"] >>= unlines >>= return
|
2 | work = ["Get", "this", "to", "work"] >>= unlines >>= return
| ^^^^^^^
After figuring out what the error message tried to tell me, I got this working:
work :: String
work = ["Get", "this", "to", "work"] >>= (arr -> unlines [arr]) >>= return
main :: IO ()
main = putStrLn work
Question:
Why do I need to pass [arr]
instead of arr
to unlines
?