-1

Such as for go(a, d). i want it to print the route as well such as route a, route b, route, c and route d

door(a, b).
door(b, c).
door(c, d).
door(b, e).
door(e, f).
door(e, g).

go(FromRoom, ToRoom):- 
door(FromRoom,ToRoom).

go(FromRoom, ToRoom) :-  
door(FromRoom, NextRoom),
 go(NextRoom, ToRoom).
false
  • 10,264
  • 13
  • 101
  • 209
user1232622
  • 253
  • 1
  • 2
  • 6

3 Answers3

2

Please try not to mix IO and logic in your predicates. It will make your code hard to test, debug, reason about and will produce very confusing output on backtraking.

Here you can keep the road in a list during the recursion.

An example would be, with a third argument:

go(FromRoom, ToRoom, [FromRoom, ToRoom]) :-
    door(FromRoom, ToRoom).

go(FromRoom, ToRoom, [FromRoom|Path]) :-
    door(FromRoom, NextRoom),
    go(NextRoom, ToRoom, Path).

A query correctly returns the path:

?- go(a, g, Path).
Path = [a, b, e, g] ;
false.

If necessary, you can then format the output list Path when you output it. But it's now a really simpler problem: format a list instead of output things during recursion.

m09
  • 7,490
  • 3
  • 31
  • 58
2

A good way is to simply turn go/2 into a relation that also takes the route into account. As is often the case when describing lists, DCGs are a good fit:

go(From, To) --> [From, To], { door(From, To) }.
go(From, To) --> [From],
        { door(From, Next) },
        go(Next, To).

Example:

?- phrase(go(a, d), Rooms).
Rooms = [a, b, c, d] ;
false.

And regarding write/1: This is rarely necessary because we can often let the toplevel take care of printing answers. format/2 is often a better fit than write/1 for formatting output if needed, for example, instead of:

write('['), write(From), write(':'), write(To), write(']')

you can write:

format("[~w: ~w]", [From, To])
mat
  • 40,498
  • 3
  • 51
  • 78
1

You need to use the write predicate for this as follows

go(FromRoom, ToRoom):-
    door(FromRoom,ToRoom),
    write('['), write(FromRoom), write(':'), write(ToRoom), write(']').

go(FromRoom, ToRoom) :-  
    door(FromRoom, NextRoom),
    write('['), write(FromRoom), write(':'), write(NextRoom), write(']'),
    go(NextRoom, ToRoom).

Alternatively, the route in the format you wanted is

go(FromRoom, ToRoom):-
   door(FromRoom,ToRoom),
   write(FromRoom), write(' route '), write(ToRoom).

go(FromRoom, ToRoom) :-  
   door(FromRoom, NextRoom),
   write(FromRoom), write(' route '),
   go(NextRoom, ToRoom).
Chetter Hummin
  • 6,687
  • 8
  • 32
  • 44
  • very good help but for go(a,d). do you know how to put it in this format route a route b route c route d yes – user1232622 Mar 25 '12 at 10:09
  • @user1232622 you can change the write output to suit your needs. I kind of prefer seeing each link. I have updated the answer – Chetter Hummin Mar 25 '12 at 10:20
  • Thanks for the help this was very hard figuring out – user1232622 Mar 25 '12 at 10:39
  • Mixing IO and logic will fail, especially in Prolog (backtracking here will still print failing branches). This solution is not good. – m09 Mar 25 '12 at 11:32
  • @Mog True. But the question did ask for a way to output to console. What do you advise? – Chetter Hummin Mar 25 '12 at 11:34
  • is it because i am missing this symbol ! – user1232622 Mar 25 '12 at 14:37
  • @user1232622 Could you please clarify? – Chetter Hummin Mar 25 '12 at 14:40
  • when i type this in | ?- go(a, d). a route b route c route dyes why doesn't it say route a and how do i add the space from route d and yes – user1232622 Mar 25 '12 at 14:59
  • @user1232622: pease try to be clear in your comments. Edit your question if needed. – m09 Mar 25 '12 at 15:04
  • ok when i type in go(a, d). the answer comes up as a route b route c route dyes can you tell me how to add the route for a so it will say route a and how can i add a space between d and yes – user1232622 Mar 25 '12 at 15:10
  • 1) if this precise output is required because of your homework assignment please add the homework tag. 2) please precise what code are you using and what is the query that led to the result you're mentionning because when I type `go(a, d)`. I don't have the same results as you do. – m09 Mar 25 '12 at 15:19
  • i was using the result in which i posted above on the top page – user1232622 Mar 25 '12 at 15:23
  • and did you try out my answer ? – m09 Mar 25 '12 at 15:31
  • its a bit confusing to me thats why i am trying to do it with the one i posted – user1232622 Mar 25 '12 at 15:35
  • well if you try to stick to your error prone ways instead of trying to actually understand what could work by asking more clarifications then I wish you good luck and leave it at that. – m09 Mar 25 '12 at 15:41