Reputation: 167
I want to define a function conditional and using recursion in Haskell (ghci 7.x) like:
partitions n k m = gfunct n k m
where
gfunct n k m
| n == 0 && k == 0 = 1
| n < m || k == 0 = 0
| otherwise = gfunct (n-m,k-1,m+1) + gfunct (n,k,m+1)
I get an error:
fib.hs:20:35:
Occurs check: cannot construct the infinite type:
a1 ~ t6 -> (t5, t6, t7) -> a1
Relevant bindings include
m :: (t5, t6, t7) (bound at fib.hs:17:23)
k :: t6 (bound at fib.hs:17:21)
n :: (t5, t6, t7) (bound at fib.hs:17:19)
gfunct :: (t5, t6, t7) -> t6 -> (t5, t6, t7) -> a1
(bound at fib.hs:17:12)
In the first argument of ‘(+)’, namely
‘gfunct (n - m, k - 1, m + 1)’
In the expression:
gfunct (n - m, k - 1, m + 1) + gfunct (n, k, m + 1)
What am I doing wrong?
Upvotes: 1
Views: 112
Reputation: 71119
A function
g a b c = 0
will return 0
if supplied by three values, a
, b
and c
; but a function
g (a,b,c) = 0
will return 0
if supplied by one value (a,b,c)
. Which is a triple of values, a
, b
and c
.
Upvotes: 3