Created
December 1, 2015 15:51
-
-
Save dasch/dcb6ff803d949308f3f1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-| Returns a list of repeated applications of `f`. | |
If `f` returns `Nothing` the iteration will stop. If it returns `Just y` then | |
`y` will be added to the list and the iteration will continue with `f y`. | |
nextYear : Int -> Maybe Int | |
nextYear year = | |
if year >= 2030 then | |
Nothing | |
else | |
Just (year + 1) | |
-- Will evaluate to [2010, 2011, ..., 2030] | |
iterate nextYear 2010 | |
-} | |
iterate : (a -> Maybe a) -> a -> List a | |
iterate f x = | |
case f x of | |
Just x' -> x :: iterate f x' | |
Nothing -> [x] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment