% length( L, N) if N is the length of list N: built-in.
% It can be used to create a list of N variables.

% len1
% reversible, but will loop forever if asked to 
%  generate a *second* list of n variables!
len1( [],0).
len1( [_ | T], N) :- len1( T, N1), N is 1 + N1.

% len2---like len1, but fully reversible
len2( L,N) :- once( len1( L,N)).
% once(P) :- P, !.  (built-in in SWI-Prolog)

% len3---result is a term that is not evaluated
len3( [],0).
len3( [_ | T], N) :- len3( T, N1), N = N1 + 1.

% len4---just like len3, but with one-goal body
len4( [],0).
len4( [_ | T], 1+N) :- len4( T, N).

% len5---use len4, and evaluate resulting term to get a number
% reversible, but with the same problem of len1
len5( L, N) :- len4( L, N1), N is N1.

% len6---use len5, and evaluate resulting term to get a number
% fully reversible, as len2 is to len1
len6( L,N) :- once( len5( L,N)).
