# Prolog - combinatorics

9 replies to this topic

### #1itsbrian

New Member

• Members
• 16 posts

Posted 04 July 2006 - 07: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:

### #2itsbrian

New Member

• Members
• 16 posts

Posted 18 July 2006 - 05:57 AM

The predicates i'm atempting to modify can be seen at http://ktiml.mff.cun...ingProlog4.html

...still waiting, atempting and hoping for a solution!

### #3Trap D

Member

• Members
• 55 posts

Posted 29 August 2006 - 10:44 PM

Is that what you were hoping ?
modified_varia(0,_).

modified_varia(N,X):-

Z is N-1,

bagof(L, varia(N,X,L), LL), write(LL), nl, modified_varia(Z,X).
I get :

Quote

5 ?- modified_varia(3, [a, b, c]).
[[a, b, c], [a, c, b], [b, a, c], [b, c, a], [c, a, b], [c, b, a]]
[[a, b], [a, c], [b, a], [b, c], [c, a], [c, b]]
[[a], [b], [c]]

Yes
Perhaps it's not exactly what you want :sad:

### #4itsbrian

New Member

• Members
• 16 posts

Posted 06 September 2006 - 04:41 AM

oops! (can't delete the duplicate i made by mistake)

### #5itsbrian

New Member

• Members
• 16 posts

Posted 06 September 2006 - 04:44 AM

:yes: That’s exactly what I need! I'm really really happy! i never thought anyone would do it. I realize that mine included 3 input parameters instead of 2, but that was only because I assumed it would need 3. I was actually starting to wonder whether programming was for me; that if I couldn’t solve one problem how would I solve another? This was a big hurdle, it got me down that I couldn’t figure it out but more that I couldn’t get help, that is, until now.

I really appreciate your help. Thanks heaps :worthy: :yes:

PPS: I'll have to study the code it's neat.

### #6Trap D

Member

• Members
• 55 posts

Posted 06 September 2006 - 08:51 PM

If you want to learn Prolog, (and it is an excellent idea) I think you should buy the excellent book "The Art of Prolog: Advanced Programming Techniques". Leon Sterling and Ehud Shapiro. MIT Press, 1994 (2nd ed).
Easyer are
"Programming In Prolog". William F. Clocksin and Christopher S. Mellish. Springer-Verlag, 2003 (5th ed).
"Prolog Programming for Artificial Intelligence". Ivan Bratko. Addison-Wesley, 2001 (3rd ed).

### #7itsbrian

New Member

• Members
• 16 posts

Posted 07 September 2006 - 03:05 AM

Those books look like excellent books particularly the first one, though it would be nice to have them all! I've put them on my list.

:happy:

### #8Ooka

Member

• Members
• 98 posts

Posted 08 September 2006 - 05:53 AM

This is out of curiousity, as I know very little about prolog or it's history...
Why prolog, despite the fact that it's been used for some high profile AI projects?

What makes it better than c++ or another standard language?

### #9Trap D

Member

• Members
• 55 posts

Posted 08 September 2006 - 12:42 PM

I don't know if it is better than C++ (I don't really know C++, just C), but for me it's another way of thinking the programmation, like Lisp/Scheme and that's important. A good programmer MUST knows different ways of thinking, when you program always the same way, your mind becomes sclerosed, and it's not good !

### #10itsbrian

New Member

• Members
• 16 posts

Posted 11 September 2006 - 06:18 AM

I've only done a hello world in c++ but it's a language i will learn.

Prolog is a declarative (non-procedural) language, C++ is procedural.
Using prolog means the programmer can concentrate more on expressing the problem itself; in a logical form rather than the procedure (algorithm) of getting to the solution.

Prolog Programming A First Course by Paul Brna
http://www.scre.ac.u...book/index.html

Why Prolog?
http://www.j-paine.org/why_prolog.html

#### 1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users