Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ifOne :: Integral a => [a] -> [a]
- ifOne xs =
- if lst == 1
- then xs
- else oddOrEven xs
- where
- lst = last xs
- oddOrEven :: Integral a => [a] -> [a]
- oddOrEven xs =
- if even lst
- then ifOne (xs ++ [div lst 2])
- else ifOne (xs ++ [lst * 3 + 1])
- where
- lst = last xs
- collatz :: Integral a => a -> [a]
- collatz x =
- if x < 1
- then error "An integer greater than zero is required!"
- else ifOne [x]
- main = print $ collatz 104
- -- [104,52,26,13,40,20,10,5,16,8,4,2,1]
Add Comment
Please, Sign In to add comment