如何更新prolog中的列表?

时间:2017-12-06 00:50:56

标签: prolog

当用户添加项目然后显示列表时,程序将输出列表而不考虑用户添加的项目在我看来,项目并未真正添加。我需要你的帮助。而且..

  • 撤消有什么用?我只是想确定我的信息
  • 欢迎任何改进此代码的批评 - 如果您曾经想出该计划的目的是什么 -

%00:33 du bist toll! :-dynamic item/1.

item([coffee,milk,chocolate,apple,juice]).

menu:-
 nl, write("**SHOPPING LIST **"),nl,
 write('d - Display all items.'), nl,
 write('a - Add an item to the list.'), nl,
 write('e - Exit.'),
 write('-------------------------------'), nl,
 write('Enter your choice: '),
 read(X), check(X),(X=e;menu).

check(a):-
    nl,
 write('Enter the item you want to add to the shopping list'), nl,

 %adding to list operations
  read(I),
    item(L),not(member(I,L)),
    retract(item(L)),
    assert(item([I|L])),
  write('The item has been added').

check(d):-
    nl,
    item(L),
 displayL(L).

displayL([]).
displayL([X|Y]):- write(X),nl, displayL(Y).


check(e):-.

item([coffee,milk,chocolate,apple,juice]). menu:- nl, write("**SHOPPING LIST **"),nl, write('d - Display all items.'), nl, write('a - Add an item to the list.'), nl, write('e - Exit.'), write('-------------------------------'), nl, write('Enter your choice: '), read(X), check(X),(X=e;menu). check(a):- nl, write('Enter the item you want to add to the shopping list'), nl, %adding to list operations read(I), item(L),not(member(I,L)), retract(item(L)), assert(item([I|L])), write('The item has been added'). check(d):- nl, item(L), displayL(L). displayL([]). displayL([X|Y]):- write(X),nl, displayL(Y). check(e):-.

0 个答案:

没有答案