Haskell: A Great Procedural Language
CRANK

We are starting to see the benefits of side effects as first class values, so let’s shift to a higher gear. We have seen that Haskell allows us to store side effect objects in variables without accidentally executing their effects. The next step is storing these side effect objects in data structures.We could, for example, create a list of three different ways to get the username of the currently logged in user.In[20]:getting_usernames :: [IO (Maybe String)] getting_usernames = [lookupEnv "USER", lookupEnv "LOGNAME", lookupEnv "SUDO_USER"] This list cannot be executed for its side effects directly, because the list itself is not a side effect – it’s a list of side effects. There are library functions to deal with this, though. One isIn[21]:sequenceA :: [IO a] -> IO [a] This is what we need in this case: it takes a list of side effect objects and creates a new side effect object that executes all the side effects of the list, and then produces a list of all the values produced by th…

entropicthoughts.com
Related Topics: Haskell