I'm still starting to explore Haskell. I know this code "runs" in the IO
monad. When it goes from the l <- ...
line to the next one, the IO - bind
is called.
One could think that because Haskell is lazy, the l
is never evaluated. But "bind
" always evaluates the previous command, is that right? Because the program produces the "file-not-found" error.
main = do
l <- mapM readFile [ "/tmp/notfound" ]
return ()
Laziness (also known as indolence) is disinclination to activity or exertion despite having the ability to act or to exert oneself. It is often used as a pejorative; terms for a person seen to be lazy include "couch potato", "slacker", and "bludger".
Lifestyle causes of laziness For example, a poor diet, too much alcohol and lack of good quality sleep can all leave you feeling tired and unmotivated. Stress can also lead to poor sleep, which in turn can make you feel tired and lacking motivation.
Taking small steps toward better self-care, increased energy, improved goals, and healthy boundary setting can help you stop feeling lazy in no time. Prioritizing and taking consistent action steps are the key to long-term change and there is no better time than now to take those first steps.
Procrastination and laziness are two different concepts: procrastination involves delaying unnecessarily, whereas laziness involves being voluntarily unwilling to exert necessary effort.
One could think that because Haskell is lazy, the l is never evaluated.
Yes, and it never is evaluated. However, due to the definition of (>>=)
in IO
, the action readFile "/tmp/notfound"
is executed, and that means the runtime tries to open the file. If there is no such file, a "File not found" error is raised. If there were such a file, it would be opened, but its contents would not be read until demanded. In the above, they are not demanded, so the contents will not be read.
What is evaluated here (and even executed) is the action producing l
. Since the file doesn't exist, that raises an error.
If you expand the do
notation in your code, you get:
main = (mapM readFile ["/tmp/notfound"]) >>= (\l -> return ())
So yes, l
is never evaluated, but that doesn't mean that the call to mapM
is never evaluated. >>=
always needs to evaluate its left operand in order to produce a value at least to some degree (at least in the IO monad and in any other monad that comes to mind).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With