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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

find all right for block 1

Status
Not open for further replies.

PrologMan

Programmer
Feb 13, 2007
1
0
0
GB
Hi every one ,I am happy to join you,,,,
I have question:
I have this database which is Blocks world.I am trying to define a predicate for finding all blocks to the right of a given block as list.The DataBase as follow:
on(b,c).
on(d,e).
on(e,g).

desk(a).
desk(b).
desk(d).
desk(f).

right_to(a,b).
right_to(b,d).
right_to(d,f).

I have struggled to find all right blocks for a given block and I have started like this:
all_right(X,Out):-
setof(X,right(X,Y),Out)
,but I need all blocks not just the blocks on the desk....

I will appreciate your help.


 
This sounds very similar to a coursework I am doing at present , which was advised to use the built in function setof/3 .

If you do a help(setof). in swiProlog it will provide information on how this is defined.

Hope this helps

XXX
Atilla
 
If you need any more help mike will be more than happy to help.

lots of love Atilla
 
what you need to do is define a predicate for finding all the blocks on a stack.

I am also assuming u need to find all the blocks to the right of a block even if the block is on a stack. So you need to find the base.

Then apply youre right_to predicate to the base.

So you may end up with two predicates possible one predicate if the block is a desk block and one if the block is stacked upon another block.

Then you would need to apply setof to both right_to and stack to give you all the blocks right to any given block.


 
hi, cant find the predicate setof/3 i.e. not defined anywhere, type in help(setof). but does not show me predicate.

I am trying to find all blocks on a desk?


thank you for your time,
 
setof(+Template, +Goal, -Set)
Equivalent to bagof/3, but sorts the result using sort/2 to get a
sorted list of alternatives without duplicates.

Is what i get, so i guess I'll have to hold ya hand.
+Template = the variable u want ie X, goal is the goal i.e a predicate that must be satisfied to be true, set = the set of all the variables that occur when the goal is true.

Example

allondesk(Y) :- setof(X,desk(X),Y).

This should pipe out to youre display a list of all the blocks that are on the desk.

Now if you want say all the blocks that arn't on the desk
lets call em stacked blocks

you would have something like

stacked(A) :- setof(X,Y^on(Y,X),A).
which would pipe out a list of all the stacked blocks

*****Hand-holding stops here****

Now I am not going to give you the code for combining them together but its very straight forward, and combining them will give you every block on the desk.

I hope this helps
 
hello,
I am trying to find a block which is the rightmost of a given block, but instead of finding the right I am finding the left, well sort of, I was hoping for some direction on how to do this . I used right_to when defining it.

thank you for your time
 
ok
how about using set_of on youre right_to function.
This should give u the list of each block on the desk starting from the one on the left.

You dont have to do it this way but using the built in predicate last can help you find the last one in the list.

In SWIProlog enter help(last). Which details how the last predicate is to be used.

Again I'm not going to write the code for you, you need to learn it yourself.

Hope this helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top