Previous Up

17.8  Hybrid Exercise

Build a hybrid algorithm to create lists whose elements all differ by at least 2. Try lists of length 3,5,7,8. To test its performance, reduce the domains thus: ic:(List::1..TwoL-2) so the program tries all possibilities before failing.

Use the following skeleton:

differ(Length,List) :-
    length(List,Length),
    TwoL is 2*Length,
    ic:(List::1..TwoL-1),
    alldiff(List,TwoL,Bools),
    [To be completed]

alldiff(List,Length,Bools) :-
    (  fromto(List,[X|Rest],Rest,[]),
       fromto([],BIn,BOut,Bools),
       param(Length)
    do
        diffeach(X,Rest,Length,BIn,BOut)
    ).
    
diffeach(X,List,Length,BIn,BOut) :-
        (foreach(Y,List),
         fromto(BIn,TB,[B|TB],BOut),
         param(X,Length)
        do
            diff2(X,Y,Length,B)
        ).

Previous Up