itsbrian
07-04-2006, 01:11 AM
I have tried various prolog forums to get help but i don't seem to be getting any. :wallbash: Maybe people think i'm trying to get someone else to do my homework but that's not the case. I've completed an introductory course in prolog which was an AI course which merely introduced us to prolog and lisp programming. Now i'm on holidays due to recommence study next week and won't be doing prolog again. Since I enjoy prolog and am trying to keep from forgetting it altogether i'm trying to build my own small project but find i've still so much more to learn. :unsure:
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).
Using the above predicates and the 'query' below:
varia(3, [a, b, c],L3); varia(2, [a, b, c],L2); varia(1, [a, b, c],L1).
gives the following result:
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] ;
However, i wish to simply call with one predicate, such as:
modified_varia(3, [a, b, c],L):
(note I've ommitted unbound variables in the following output for clarity only)
i.e. Output similar to:
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];
My attempt at implementing the predicate "varia" recursively to achieve the above output is flawed: :blink:
modified_varia(0,_,_).
modified_varia(N,X,Y):- Z is N-1, varia(Z,X,Y), modified_varia(Z,X,Y).
If anyone out there can help me to code this i would most appreciate it. :worthy:
Thanks,
Brian :mellow:
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).
Using the above predicates and the 'query' below:
varia(3, [a, b, c],L3); varia(2, [a, b, c],L2); varia(1, [a, b, c],L1).
gives the following result:
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] ;
However, i wish to simply call with one predicate, such as:
modified_varia(3, [a, b, c],L):
(note I've ommitted unbound variables in the following output for clarity only)
i.e. Output similar to:
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];
My attempt at implementing the predicate "varia" recursively to achieve the above output is flawed: :blink:
modified_varia(0,_,_).
modified_varia(N,X,Y):- Z is N-1, varia(Z,X,Y), modified_varia(Z,X,Y).
If anyone out there can help me to code this i would most appreciate it. :worthy:
Thanks,
Brian :mellow: