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 ()
Yes, and it never is evaluated. However, due to the definition of
(>>=)inIO, the actionreadFile "/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.