I want to find a Composition of Integer in C and Haskell I find this code for Haskell which works fine:
composition 0 = [[]]
composition n = [x:rest | x <- [1..n], rest <- composition (n-x)]
Sample output of this Code Is:
*Main> composition 3
[[1,1,1],[1,2],[2,1],[3]]
*Main> composition 4
[[1,1,1,1],[1,1,2],[1,2,1],[1,3],[2,1,1],[2,2],[3,1],[4]]
but I could not develop an equivalent in C: I tried to create recursive function in C but I don't know how to use equivalent of Haskell :[] in C
void composition(int n)
{
int j;
if (n==0)
return;
for(j=1;j<=n;j++){
printf ("%d",j);
composition(n-j);
printf("\n");
}
}
sample Output of this code is:
Composition is
111
2
21
3
The correct composition for 3 is
1 1 1
2 1
1 2
3