0
dynamic( [im_at/1, door/1]).

im_at(car).

door(false).

/* Facts*/

path(car,forward,gasstation):-
   door(true),write('Wellcome to Gas Station'),nl,
   write('You can buy a newspaper or a drink!').
path(car,forward,gasstation):-
   write('You need to open the Car door '),nl,
   !,fail.
path(gasstation,back,car):-
   door(true),write('You got back in the car'),nl.
path(gasstation,back,car):-
   write('You need to open the car door'),nl.

open_cardoor:-
          door(true),
          write('You already opend the door!'),
          nl, !.
open_cardoor:-
    im_at(car),
    assertz(door(true)),
    retract(door(false)),nl,
    write('You openned the car door!'),nl,!.

forward:-go(forward).

back:-go(back).

go(Direction) :-
    im_at(Here),
    path(Here, Direction, There),
    retract(im_at(Here)),
    assert(im_at(There)),
    !.

buy(drink) :-
    im_at(gasstation),
    write('Added a drink to bag.'), nl,
    !.
buy(newspaper) :-
    im_at(gasstation),
    write('Added a newspaper to bag.'), nl,
    !.
buy(_):-
    write('You need to go to Gas Station to buy!'),nl.

start:- write('All that driving made me thirsty, you?'),nl.
4

1 回答 1

0

我假设您使用的是 SWI-Prolog。为了声明一系列谓词是动态的,您应该使用手册中记录的语法。在您的情况下,正确的条款是

:- dynamic im_at/1, door/1.

或者

:- dynamic([im_at/1, door/1]).

程序的第一行试图通过声明这是一个事实来修改的定义。这是禁止的,因为它是静态的(即禁止您修改程序中的定义)。dynamic/1dynamic([im_at/1, door/1])dynamic/1dynamic/1

于 2020-01-26T01:14:35.697 回答