I am trying to implement a tail recursive version of factorial:
let{factorial 0 n = n; factorial x n = factorial (x-1, n * x)}
I get this:
<interactive>:1:41:
Occurs check: cannot construct the infinite type: t1 = t1 -> t1
In the return type of a call of `factorial'
In the expression: factorial (x - 1, n * x)
In an equation for `factorial':
factorial x n = factorial (x - 1, n * x)
<interactive>:1:52:
Occurs check: cannot construct the infinite type: t0 = (t0, t1)
In the first argument of `(-)', namely `x'
In the expression: x - 1
In the first argument of `factorial', namely `(x - 1, n * x)'
<interactive>:1:61:
Occurs check: cannot construct the infinite type: t1 = (t0, t1)
In the second argument of `(*)', namely `x'
In the expression: n * x
In the first argument of `factorial', namely `(x - 1, n * x)'
How am i constructing an infinite type here? (using GHCi 7.0.1)
I'm not a strong Haskell programmer, but I think you want to rewrite
factorial x n = factorial (x-1, n * x)
as
factorial x n = factorial (x-1) (n * x)
Since (x-1, n * x)
is a pair type, which isn't what you want.
Hope this helps!