In this recipe, we will revisit list and look at it as a monad. List is a monad, and we will work with a few examples of how to work with a list with monadic syntax and functions.
List as monad
How to do it...
- Create a new project list-as-monad using the simple Stack template.
- Open src/Main.hs and edit it.
- Add the following import for monad:
import Control.Monad
- Write a function that takes an integer x and returns a list of all integers starting with x ([x, x+1, x+2,...]):
nexts :: Num a => a -> [a] nexts x = do x : nexts (x+1)
- Write a function that takes two lists and returns all ordered pairs from this list:
pairs :: [a] -> [b] -> [(a,b)] pairs xs ys = do...