You might think that since Haskell is lazy, l is never evaluated.
Yes, and he is never evaluated. However, due to the definition (>>=) in IO , the readFile "/tmp/notfound" action readFile "/tmp/notfound" is executed, which means that the runtime is trying to open the file. If there is no such file, the "File not found" error occurs. If such a file were opened, it would be open, but its contents will not be read until it is required. In this case, they are not required, so the contents will not be read.
What is evaluated here (and even performed) is the action that produces l . Since the file does not exist, this causes an error.
Daniel Fischer
source share