ancestor(Ancestor, Person) :- parent(Parent, Person), ancestor(Ancestor, Parent). (tail recursion) ancestor(Ancestor, Person) :- parent(Ancestor, Perso). (base case)
Make head and tail of the following :
[X | [Y]] [[ a,b,c | d],e | [ f,g | X]] [ a | [ b | [ c | [d]]]]
Similarly with lists :
list_length([],0). list_length([H|T], N) :- list_length(T,N1), N is N1+1.
This can be explained as :
The recursive use of the list_length relationship allows the length to be calculated. Each time, we are adding on one element (the head) to our calculation.