I’ve discovered that when doing IO in Haskell, variables that are assigned using the <- operator are only in scope for the statements immediately after them – not in where clauses.
For example:
main :: IO()
main = do
s <- getLine
putStr magic
where
magic = doMagic s
This won’t work as s is not in scope. I did some research to confirm it and found this article:
Unlike a
letexpression where variables are scoped over all definitions, the variables defined by<-are only in scope in the following statements.
So how can I make s available for use in the where clause?
In addition to the general let form, there’s a special let form for use with do syntax you can use instead:
magicis available in all following lines in the block.