Skip to content

Instantly share code, notes, and snippets.

@splch
Created February 24, 2022 07:16
Show Gist options
  • Select an option

  • Save splch/9cead7186cae95ca0857359e1985b0c7 to your computer and use it in GitHub Desktop.

Select an option

Save splch/9cead7186cae95ca0857359e1985b0c7 to your computer and use it in GitHub Desktop.
Creates collatz sequences in Haskell
collatz :: Integer -> Integer
collatz n =
if even n
then n `div` 2
else 3 * n + 1
collatzSeq :: Integer -> [Integer]
collatzSeq n =
if n == 1 -- base case
then 1 : []
else n : collatzSeq (collatz n) -- recursive call
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment