문제

:- use_module(library(lists)).

friendof(mark, frank).
friendof(mark, sylvie).
friendof(john, sylvie).
friendof(marie, chris).


symmetrical(X, Y) :- friendof(X, Y).
symmetrical(X, Y) :- friendof(Y, X).

:- dynamic(friendlist/1).

friends(Friend, LL)  :-
   asserta(friendlist([])),
   friends2(Friend)
   retract(friendlist(LL)).
friends2(F) :-

What I have to do is, using friends as it is (so, using friends2/1), get all the friends of mark as such:

?-friends (mark, X). would give this list, (take note of the order in which they appear): X = [sylvie, frank]

I was told many things, of which I should be doing symmetrical(..),retract(..),assert(..),fail.

But it's just not working to put the list back in friendlist from friends2/1. Can someone show me how it's done?

It's really important!

Thanks!

EDIT1:

These are some attempts at getting it to work.

friends2(F) :- findall(B,symmetrical(A,B),B).
symmetrical(F,Y),retract(friendlis([Y:_])),assert(...
%symmetrical(..),retract(..),assert(..),fail.
도움이 되었습니까?

해결책

Maybe you're doing it much more complex that actually is.

Most importantly: assert/retract should not be used for computing temporary relations in DB. Use them only to change knowledge - that is, well structured and meaningful relations that need to be persistent for the duration of the program.

For your task, try

friends_of(Person, Friends) :-
  setof(Friend, (friendof(Person, Friend) ; friendof(Friend, Person)), Friends).

edit Of course, I don't understand why you need to do it so much more complicated... maybe you need to show you are able to issue modification to DB ? I fear you will discover that such problem (keeping a coherent db state) is much more difficult than foreseen, and learning to assert/retract useless relations will not help you much. Anyway...

friends(Friend, LL)  :-
   asserta(friendlist([])),
   friends2(Friend),
   retract(friendlist(LL)), !.

friends2(F) :-
   ( friendof(F, P) ; friendof(P, F) ),
   retract(friendlist(L)), 
   assert(friendlist([P|L])),
   fail.
friends2(_).

could work for you...

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top