Haskell versus I/O – a thought experiment
Most programming languages provide a similar approach to deal with I/O-related tasks. They provide functions, procedures, or methods that are essentially indistinguishable from other functions, procedures, or methods in the language. Not so in Haskell, where both lazy evaluation and Haskell’s principled approach toward functions mean that I/O should not be treated in the same way.
Before diving into Haskell’s approach toward I/O, this section explains why the conventional approach does not work. As a thought experiment, we will use two minimal I/O functions – getChar
, which reads a single character from the standard input, and putChar
, which writes a single character to the standard output. Following other languages, the types of these functions could be getChar :: () -> Char and putChar :: Char -> ()
.
Let us proceed with a hypothetical world where Haskell has the aforementioned two functions with those...