4 ms·
Or perhaps like this if you do not want to use `reverse` and want it to be tail recursive: collatz :: Int -> [Int] collatz x = go id x [] where
by secretguy 13y ago
Or perhaps like this if you do not want to use `reverse`
and want it to be tail recursive:
collatz :: Int -> [Int]
collatz x = go id x []
where
go :: ([Int] -> [Int]) -> Int -> [Int] -> [Int]
go f 1 = f . (1:)
go f n =
if even n
then go (f . (n:)) (n `div` 2)
else go (f . (n:)) (3 * n + 1)