Skip to content

Instantly share code, notes, and snippets.

@dasch
Created December 1, 2015 15:51
Show Gist options
  • Save dasch/dcb6ff803d949308f3f1 to your computer and use it in GitHub Desktop.
Save dasch/dcb6ff803d949308f3f1 to your computer and use it in GitHub Desktop.
{-| 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