Created
December 10, 2011 06:15
-
-
Save shugo/1454684 to your computer and use it in GitHub Desktop.
Collatz sequence
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
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