- 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:
- 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 x <- xs y <- ys return (x,y)
- Write a partitioning function using list comprehension. The same function is also written using the monadic syntax:
partition :: (a -> b -> Bool) -> [a] -> [b] -> [(a,b)] partition f xs ys = [ (x, y) | x <- xs, y <- ys, f x y] partition1 ...