Re: Why does this recursion not work?



Anders Lindén wrote:
%to get the tail of the sequence

tail(sekv(D,T),T).

%find the last element:

last(nil,nil).

last(sekv(D,nil),D). %only one element left

%the recursion

last(sekv(D,R),R2):- last(tail(R,R2),R2).

Why does this recursion not work?


To save ink, I will replace your 'sekv' functor with the single-letter functor 'd'.


 What is the "tail" and what is the "last elment" in each of these cases:

    nil

    d(X,Y)

    d(a,d(X,Y))

    d(a,d(X,d(Y,nil)))
.