Brian Dolan
Brian Dolan

Reputation: 3136

Prolog predicate with multiple lists

I'm using swipl but working the adventure tuorial from Amzi!. The syntax difference is causing me a headache. After setting up the facts, I have these statements

list_things(Place) :-
    location(X, Place),
    tab(2),
    write(X), nl,
    false.
   

list_connections(Place) :-
    connect(Place, X),
    tab(2),
    write(X), nl,
    false.

look :-
    here(Place),
    write('You are in the '), write(Place), nl,
    write("You can go to: "), nl,
    list_connections(Place), nl,
    write('You can see: '), nl,
    list_things(Place).

When I run look., it only outputs the first list from list_connections. That is

?- look.
You are in the kitchen
You can go to:
  office
  cellar
  dining room
false.

Now, I understand the false at the end of the function is terminating the look, but I don't know the correct syntax in swipl.

Upvotes: 0

Views: 64

Answers (1)

TessellatingHeckler
TessellatingHeckler

Reputation: 28993

Now, I understand the false at the end of the function is terminating the look

The false at the end of list_connections(Place) forces it to backtrack and retry, writing all the connected places as it does so. Once it has got through them all and failed for the last time, I think there is supposed to be another rule for it:

list_connections(_).

I see it here in Amzi! chapter 5:

Screenshot of Amzi chapter 5 page

That rule is a "succeed for anything" rule which will allow look to carry on and then list_things can run. And there is a similar one for list_things.

Upvotes: 1

Related Questions