我们的老师添加了一个代码,用于添加,删除和显示列表。但这是行不通的,当我说添加或删除时,它仅在控制台中显示“ False”。我该如何克服这个错误?
...
del_data(Data, List, NewList):- find(Data, List, [], ListAfterDeletion), NewList = ListAfterDeletion.
find([_ | _], [], _):-write("There is no such element").
find([DataHead | DataTail], [ListHead | ListTail], NewList, ListAfterDeletion):- (similar(DataHead, DataTail, ListHead) -> reverse(NewList, Reversed),
append(Reversed, ListTail, Result),
ListAfterDeletion = Result;
find([DataHead | DataTail], ListTail, [ListHead | NewList], ListAfterDeletion)).
similar(DataHead, DataTail, [HeadofListHead | TailofListHead]):- DataHead == HeadofListHead, DataTail == TailofListHead.
main(L):-
write("Please select your operation:\n1. Add\n2. Delete\n3. Display\n4. Exit\n"),
read(Operation),
(Operation is 1 ->write("Please input your data to add:\n"), read(Data), add_data(Data, L, NewList), main(NewList) ;
(Operation is 2 -> write("Please input your data to delete\n"), read(Data), del_data(Data, L, NewList), main(NewList) ;
(Operation is 3 -> write(L), write("\n"), main(L) ;
write("Goodbye")))).