Skip to content

Instantly share code, notes, and snippets.

@shugo
Created December 10, 2011 06:15
Show Gist options
  • Save shugo/1454684 to your computer and use it in GitHub Desktop.
Save shugo/1454684 to your computer and use it in GitHub Desktop.
Collatz sequence
collatz' :: Int -> Maybe (Int, Int)
collatz' 0 = Nothing
collatz' 1 = Just (1, 0)
collatz' n
| n `mod` 2 == 0 = Just (n, n `div` 2)
| otherwise = Just (n, 3 * n + 1)
collatz :: Int -> [Int]
collatz n = unfoldr collatz' n
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment