I’m using: SWIProlog version(SWISH)
I want the code to:

all_cousins(Person, Degree, ListOfCousins) that takes as arguments (1) the reference person, (2) the degree of cousin relationship (first cousin, second cousin,…) given as an integer and returns the corresponding list of cousins in ListOfCousins.
Ex: all_cousins(john, 1, ListOfCousins) unifies ListOfCousins with a list of all of John’s first cousins.
But what I’m getting is code that infinitely adds the same cousin to an empty list. I’m having trouble recursively gathering all of the cousins I believe. Also, I’m having trouble understanding how I build a list through another rule as I understand lists only have local scope and are emptied with every recursive call.
My code looks like this:
/* Facts */
male(shawn).
male(geo).
male(mark).
male(jake).
female(tia).
female(sussane).
female(derue).
female(debbie).
female(ashley).
female(phadra).
female(naveah).
/*1 is child of 2*/
child(jake, debbie).
child(geo, shawn).
child(deshaw, shawn).
child(shawn, debbie).
child(naveah, deshaw).
child(mark, derue).
child(debbie, derue).
child(derue, sussane).
child(phadra, ashley).
child(ashley, mark).
child(tia, jake).
child(sean,tia).
child(sarah, jake).
child(john, sean).
child(greatgrandaunt, sussane).
child(jessica, greatgrandaunt).
child(chad, jessica).
child(ned,chad).
child(stanley,ned).
child(hector,stanley).
child(seth,phadra).
child(angelina, seth).
/* Rules */
/*first cousin must share the same grandparent also checks if the parents
* are siblings.*/
firstCousin_of(X,Y):
child(X,Z),
child(Z,C),
child(Z1,C),
child(Y,Z1),X \= Y,
sibling_of(Z1,Z).
firstCousin_of(X,Y):
child(X,Z),
child(Z,C),
child(Z1,C),
child(Y,Z1),
sibling_of(Z1,Z),
firstCousin_of(Y,X).
firstCousin1_of(X,Y):
child(X,Z),
child(Z,C),
child(C,P),
child(Z1,P),
child(Y,Z1),X \= Y,
sibling_of(Z1,C).
firstCousin1_of(X,Y):
child(X,Z),
child(Z,C),
child(C,P),
child(Z1,P),
child(Y,Z1),
sibling_of(Z1,C),
secondCousin_of(Y,X).
firstCousin2_of(X,Y):
child(X,Z),
child(Z,C),
child(C,T),
child(T,H),
child(S,H),
child(Y,S),X \= Y,
sibling_of(T,S).
firstCousin2_of(X,Y):
child(X,Z),
child(Z,C),
child(C,T),
child(T,P),
child(E,P),
child(Y,E),
sibling_of(T,P),
firstCousin_of(Y,X).
firstCousin1d_of(X,Y):
child(X,Z),
child(Z,C),
child(Z1,C),
child(H,Z1),
child(Y,H),X \= Y,
sibling_of(Z1,Z).
firstCousin1d_of(X,Y):
child(X,Z),
child(Z,C),
child(Z1,C),
child(H,Z1),
child(Y,H),
sibling_of(Z1,Z),
firstCousin_of(Y,X).
firstCousin2d_of(X,Y):
child(X,Z),
child(Z,C),
child(Z1,C),
child(H,Z1),
child(T,H),
child(Y,T),X \= Y,
sibling_of(Z1,Z).
firstCousin2d_of(X,Y):
child(X,Z),
child(Z,C),
child(Z1,C),
child(H,Z1),
child(T,H),
child(Y,T),
sibling_of(Z1,Z),
firstCousin_of(Y,X).
/*second cousin once removed up*/
secondCousin1_of(X,Y):
child(X,Z),
child(Z,C),
child(C,T),
child(T,H),
child(S,H),
child(W,S),
child(Y,W),X \= Y,
sibling_of(T,S).
secondCousin1_of(X,Y):
child(X,Z),
child(Z,C),
child(C,T),
child(T,P),
child(E,P),
child(W,E),
child(Y,W),
sibling_of(T,P),
firstCousin_of(Y,X).
/*must have the same great grand parent*/
secondCousin_of(X,Y):
child(X,Z),
child(Z,C),
child(C,P),
child(Z1,P),
child(F,Z1),
child(Y,F),X \= Y,
sibling_of(Z1,C).
secondCousin_of(X,Y):
child(X,Z),
child(Z,C),
child(C,P),
child(Z1,P),
child(F,Z1),
child(Y,F),
sibling_of(Z1,C),
secondCousin_of(Y,X).
/*must have the same great great grand parent*/
thirdCousin_of(X,Y):
child(X,Z),
child(Z,C),
child(C,P),
child(P,G),
child(Z1,G),
child(Z2,Z1),
child(Z3,Z2),
child(Y,Z3),X \= Y,
sibling_of(P,Z1).
thirdCousin_of(X,Y):
child(X,Z),
child(Z,C),
child(C,P),
child(P,G),
child(Z1,G),
child(Z2,Z1),
child(Z3,Z2),
child(Z4,Z3),
sibling_of(P,Z1),
thirdCousin_of(Z4,Y).
thirdCousin1_of(X,Y):
child(X,Z),
child(Z,C),
child(C,P),
child(P,G),
child(Z1,G),
child(Z2,Z1),
child(Z3,Z2),
child(Z4,Z3),
child(Y,Z4),X \= Y,
sibling_of(P,Z1).
thirdCousin1_of(X,Y):
child(X,Z),
child(Z,C),
child(C,P),
child(P,G),
child(Z1,G),
child(Z2,Z1),
child(Z3,Z2),
child(Z4,Z3),
child(Y,Z4),
sibling_of(P,Z1),
thirdCousin_of(Z4,Y).
thirdCousin2_of(X,Y):
child(X,Z),
child(Z,C),
child(C,P),
child(P,G),
child(Z1,G),
child(Z2,Z1),
child(Z3,Z2),
child(Z4,Z3),
child(Z5,Z4),
child(Y,Z5),X \= Y,
sibling_of(P,Z1).
thirdCousin2_of(X,Y):
child(X,Z),
child(Z,C),
child(C,P),
child(P,G),
child(Z1,G),
child(Z2,Z1),
child(Z3,Z2),
child(Z4,Z3),
child(Z5,Z4),
child(Y,Z5),
sibling_of(P,Z1),
thirdCousin_of(Z4,Y).
/*must have the same great grand parent*/
secondCousin1d_of(X,Y):
child(X,Z),
child(Z,C),
child(C,P),
child(Z1,P),
child(F,Z1),
child(F1,F),
child(Y,F1),X \= Y,
sibling_of(Z1,C).
secondCousin1d_of(X,Y):
child(X,Z),
child(Z,C),
child(C,P),
child(Z1,P),
child(F,Z1),
child(F1,F),
child(Y,F1),
sibling_of(Z1,C),
secondCousin_of(Y,X).
secondCousin2d_of(X,Y):
child(X,Z),
child(Z,C),
child(C,P),
child(Z1,P),
child(F,Z1),
child(F1,F),
child(F2,F1),
child(Y,F2),X \= Y,
sibling_of(Z1,C).
secondCousin2d_of(X,Y):
child(X,Z),
child(Z,C),
child(C,P),
child(Z1,P),
child(F,Z1),
child(F1,F),
child(F2,F1),
child(Y,F2),
sibling_of(Z1,C),
secondCousin_of(Y,X).
list_append(A,T,T) : list_member(A,T),!.
list_append(A,T,[AT]).
list_member(X,[X_]).
list_member(X,[_TAIL]) : list_member(X,TAIL).
sibling_of(Y,Z):Y \= Z,
child(Y,D),child(Z,D).
all_cousins(Person, 1, ListOfCousins):
firstCousin_of(Person,Cousin),
list_append(Cousin,[],ListOfCousins).
all_cousins(Person, 2, ListOfCousins):
secondCousin_of(Person,Cousin),
list_append(Cousin,[],ListOfCousins).
all_cousins(Person, 3, ListOfCousins):
thirdCousin_of(Person,Cousin),
list_append(Cousin,[],ListOfCousins).
all_cousinsRemoved(Person, 1, removed(1, up), ListOfCousins):
firstCousin1_of(Person,Cousin),
list_append(Cousin,[],ListOfCousins).
all_cousinsRemoved(Person, 1, removed(2, up), ListOfCousins):
firstCousin2_of(Person,Cousin),
list_append(Cousin,[],ListOfCousins).
all_cousinsRemoved(Person, 1, removed(1, down), ListOfCousins):
firstCousin1d_of(Person,Cousin),
list_append(Cousin,[],ListOfCousins).
all_cousinsRemoved(Person, 1, removed(2, down), ListOfCousins):
firstCousin2d_of(Person,Cousin),
list_append(Cousin,[],ListOfCousins).
all_cousinsRemoved(Person, 2, removed(1, up), ListOfCousins):
secondCousin1_of(Person,Cousin),
list_append(Cousin,[],ListOfCousins).
all_cousinsRemoved(Person, 2, removed(1, down), ListOfCousins):
secondCousin1d_of(Person,Cousin),
list_append(Cousin,[],ListOfCousins).
all_cousinsRemoved(Person, 2, removed(2, down), ListOfCousins):
secondCousin2d_of(Person,Cousin),
list_append(Cousin,[],ListOfCousins).
all_cousinsRemoved(Person, 3, removed(1, down), ListOfCousins):
thirdCousin1_of(Person,Cousin),
list_append(Cousin,[],ListOfCousins).
all_cousinsRemoved(Person, 3, removed(2, down), ListOfCousins):
thirdCousin2_of(Person,Cousin),
list_append(Cousin,[],ListOfCousins).
isSingleChild(Person):
child(Person,Parent),
child(Unknownchild , Parent),
sibling_of(Unknownchild, Person),
!, fail ; true.