I just started learning Haskell recently, and it's a really cool language, but I'm confused about how I should "return" value in a function once I call a do block.
Specifically, consider the following function parseMessage,
parseMessage :: String -> LogMessage
parseMessage string = do
let brokenLine = words string
let label = take 1 brokenLine
if label == "E" then Error
else if label == "I" then Info
else if label == "W" then Warning
else return Unknown
where Error, Info, and Warning are three constructors of the type LogMessage. I think there are two problems with the code - both of which I don't know how to fix:
- How to compare if two strings are equal? Is what I'm doing correct?
- How to "return"
Error,Info,Warningin the if-else statement?
Also, I know that I can usually use "guards" like this:
parseMessage :: String -> LogMessage
parseMessage string
| label == "E" = Error
| label == "I" = Info
| label == "W" = Warning
| otherwise = Unknown
but where should I process
let brokenLine = words string
let label = take 1 brokenLine
Sorry if this is something really basic! Thanks in advanc3 :))