Haskell: A Great Procedural Language

submited by
Style Pass
2024-11-27 06:30:03

These sound like dismissals or absurdities from the outside, but once you learn what they really mean, they take on a new light. In this article, I want to explain the third. (See the appendix if you are curious about the first two.)

This article really, really tried to become a monad i/o tutorial, but I think I stopped it in time.11 By that I mean I had to rewrite it twice and delete large chunks of monad i/o tutorial material. Here, we are going to jump right in and focus on the interesting stuff.

Effectful computations in Haskell are first class values. This means we can store them in variables or data structures for later use. There is a Haskell function

which, when given two integers as arguments, picks a random integer between them. We can put calls to this function into a list, like so:

This is a list of two calls to randomRIO. What surprises non-Haskellers is that when this list is created, no random numbers are generated. Coming from other programming languages, we are used to side effects (such as random generation) being executed directly when the side effectful function is called.22 You may think Haskell is different here due to laziness, but that’s also not true. Even if we put these calls into a strict data structure, no randomness would happen.

Leave a Comment