Home > Archive > Prolog > September 2006 > Combinatorics - Recursing Permutation while decrementing
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Combinatorics - Recursing Permutation while decrementing
|
|
| wheby003 2006-06-29, 2:12 am |
| I wish to find all permutations of a list incrementing down to just one character.
Using the following predicates i wish to increment N down automatically (recalling the predicate each time starting with N then N is n-1, etc. down to N>0)
The following code does the job except i have to input N-1 each time to get reducing permutations:
varia(0,_,[]).
varia(N,L,[H|Varia]):-N>0,N1 is N-1,delete(H,L,Rest),varia(N1,Rest,Varia).
delete(X,[X|T],T).
delete(X,[H|T],[H|NT]):-delete(X,T,NT).
Basically I want to get these results with one single call; not as a disjunction of 3 calls!
?- varia(3, [a, b, c],L3); varia(2, [a, b, c],L2); varia(1, [a, b, c],L1).
L3 = [a, b, c]
L2 = _G632
L1 = _G645 ;
L3 = [a, c, b]
L2 = _G632
L1 = _G645 ;
L3 = [b, a, c]
L2 = _G632
L1 = _G645 ;
L3 = [b, c, a]
L2 = _G632
L1 = _G645 ;
L3 = [c, a, b]
L2 = _G632
L1 = _G645 ;
L3 = [c, b, a]
L2 = _G632
L1 = _G645 ;
L3 = _G619
L2 = [a, b]
L1 = _G645 ;
L3 = _G619
L2 = [a, c]
L1 = _G645 ;
L3 = _G619
L2 = [b, a]
L1 = _G645 ;
L3 = _G619
L2 = [b, c]
L1 = _G645 ;
L3 = _G619
L2 = [c, a]
L1 = _G645 ;
L3 = _G619
L2 = [c, b]
L1 = _G645 ;
L3 = _G619
L2 = _G632
L1 = [a] ;
L3 = _G619
L2 = _G632
L1 = [b] ;
L3 = _G619
L2 = _G632
L1 = [c] ;
No
My attempt at calling this predicate with recursion is flawed:
myvaria(0,_,_).
myvaria(N,X,Y):- Z is N-1, varia(Z,X,Y), myvaria(Z,X,Y).
Slowly going round the twist!
I would like the program to do this for me (with just N=3 so it decrements it for me):
varia(3, [a, b, c],L3)
varia(2, [a, b, c],L2)
varia(1, [a, b, c],L1).
Can someone please solve this so i can see where im going wrong?
Much appreciated. | |
| wheby003 2006-06-29, 4:43 pm |
| i.e. just one call with something like this:
varia(3, [a, b, c],L).
L = [a, b, c];
L = [a, c, b];
L = [b, a, c];
L = [b, c, a];
L = [c, a, b];
L = [c, b, a];
L = [a, b];
L = [a, c];
L = [b, a];
L = [b, c];
L = [c, a];
L = [c, b];
L = [a];
L = [b];
L = [c]; | |
| wheby003 2006-06-29, 4:45 pm |
| i.e. just one call with something like this:
myvaria(3, [a, b, c],L).
L = [a, b, c];
L = [a, c, b];
L = [b, a, c];
L = [b, c, a];
L = [c, a, b];
L = [c, b, a];
L = [a, b];
L = [a, c];
L = [b, a];
L = [b, c];
L = [c, a];
L = [c, b];
L = [a];
L = [b];
L = [c]; | |
|
|
|
|
|