Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Prolog syntax and lists 1

Status
Not open for further replies.

Whimsical

Programmer
Oct 24, 2012
4
0
0
US
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 */
head([X|_], X).
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
 
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).
 
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top