×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Prolog syntax and lists

Prolog syntax and lists

(OP)
I am trying to write a program that describes a scenario where a bunch of blocks are stacked on top of each other, and on 4 different "places" this is what I have so far:

block(a).
block(b).
block(c).
block(d).
block(e).
block(f).
block(g).

place(p1).
place(p2).
place(p3).
place(p4).

on(a, p1).
on(d, a).
on(g, d).
on(f, g).
on(b, p3).
on(e, b).
on(c, p4).

/*Simple Rules */
under(X, Y) :- on(Y, X).
between(X, Y, Z) :- on(Y, X), under(Z, X).
alone(X) :- <---- need help here

/* Recursive Rules */
bottom(X, Y) :- under(X, Y).
bottom(X, Y) :- under(Z, Y), bottom(X, Z).

/* List Processing */
tail([_|T], T).

/* Print out the elements of a list */
traverse([]).
traverse([X|T]) :- print(X), nl, traverse(T).

stacked([X], Y) :- on(X, Y).
stacked([X], Y) :- on(Z, Y), stacked(X, Z). <----- and here

len(X, [Y]) :- ???
len(X, [Y]) :- ???

-------------------------------------------------------------------------------------

The alone(X) rule is supposed to be true if it is the only block that is stacked on a place, but I'm not sure how exactly to write this.

The other problem I'm having is with lists, stacked is supposed to return a list of all the blocks stacked on Y, and len is supposed to return X where X is the number of elements in list Y. I just have no idea what the syntax is here or how to get the query to return a whole list instead of just 1 value. Prologs website is down right now too apparently so any help would be greatly apprecatiated

RE: Prolog syntax and lists

The first question :

CODE

alone(X) :-
% X is on a place
on(X, Pl),
place(Pl),
% nobody is on X
\+on(_, X). 
The number of elements of a list is given by the predicate lenght/2 : length([a,b], N) ==> N = 2.

The second question :

CODE

stacked(L, X) :-
% we search the first block on X
on(A, X),
% we search the others
next_on([A], L).

next_on([A|T], L) :-
% Is there a block on A ?
on(B, A),
% yes, we keep on the search
next_on([B, A | T], L).

next_on([H | T], [H | T]) :-
% there is no block on A
% the search is finished
\+on(_, H). 

RE: Prolog syntax and lists

(OP)
Thanks a ton for your help, I think I have the program mostly figured out. I am trying to do stacked as a recursive function, so far I have

stacked([], Y) :0 \on+(_, Y).
stacked([Z | _], N):- on(Z, N), stacked([], Z).

It works in cases where there are 0-1 blocks stacked on another, but returns false everytime I run it with a block that has more than 1 other block stacked on it. I feel like I'm missing a really simple piece of code but I can't put my finger on what it is

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!