I am trying to write a function that recursively determines the amount of steps needed to resolve the collatz conjecture. I want the function input to be just the starting input number for the series, with an additional counter variable to be added in the first iteration. The JS equivalent would look like:
const collatz = (v, count) => {
if (!count) return collatz(v,0)
if (v === 1) return count
if (v % 2 === 0) return collatz(v/2, count + 1)
return collatz(v*3+1, count + 1)
}
My Haskell code:
module CollatzConjecture (collatz) where
collatz :: Integer -> Maybe Integer -> Maybe Integer
collatz v () = collatz v 0
collatz 1 count = Just count
collatz v count
| even v = collatz (div v 2) next
| otherwise = collatz (v * 3 + 1) next
where next = count + 1
But the compiler complains
• Couldn't match type ‘Maybe Integer’ with ‘Integer’
Expected type: Maybe Integer
Actual type: Maybe (Maybe Integer)
• In the expression: Just count
In an equation for ‘collatz’: collatz 1 count = Just count
|
4 | collatz 1 count = Just count
| ^^^^^^^^^^
What is the error in my thinking?