Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • 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.

Students Click Here

Prolog syntax and lists

Prolog syntax and lists

Prolog syntax and lists

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:



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 */
head([X|_], X).
tail([_|T], T).

/* Print out the elements of a list */
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 :


alone(X) :-
  % X is on a place
  on(X, 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 :


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

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.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

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:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close