next up previous
Next: CLP Program Execution Up: Adding Computation Domains: CLP Previous: CLP in Practice

A More Involved Example

Find a list of length N, with numbers from 1 to N, such that the sum of the number in even position minus the sum of numbers in odd position is N. Using clp($\Re$):

len([], 0).
len([X|Xs], N + 1):- len(Xs, N).

odds_minus_evens([], 0).
odds_minus_evens([I], I).
odds_minus_evens([I, P|Rest], I - P + SumRest):-
        odds_minus_evens(Rest, SumRest).

member(X, [X|Xs]).
member(X, [Y|Ys]):- member(X, Ys).

all_numbers(0, List).
all_numbers(N, List):-
        N > 0,
        member(N, List),
        all_numbers(N - 1, List).

my_list(N, L):-
        len(L, N),
        odds_minus_evens(L, N),
        all_numbers(N, L).



Last modification: Thu Oct 7 12:04:03 MEST 1999 <webmaster@clip.dia.fi.upm.es>