0
votes

I have created a predicate that will check whether all the items in a list satisfy a condition.

For this example, the predicate checks that all elements are in multiples of two Check_Multiples/1, which works quite well.

How would I check to see what item could be added to the beginning or the end of the list, and still satisfy the predicate?

I am trying to make the return a list.

For example:

[2,4,6]

should return [8] as (as the predicate does not allow 0)

[6,8,10]

should return [4,12]

1
If the predicate, such as Check_Multiples/1, is properly defined with CLP(FD) and has well-defined rules what that predicate means, then this would simply be, begin_or_end(L, BE) :- append(L, [Y], L1), findall(X, (Check_Multiples([X|L]) ; Check_Multiples(L1)), BE)..lurker
Oops that should be, begin_or_end(L, BE) :- findall(X, (Check_Multiples([X|L]) ; append(L, [X], L1), Check_Multiples(L1)), BE).lurker
Thank you for your response I will try that. I'm afraid I don't know what CLD is and will have to read some more.My naive approach was to try to call the Check_Multiples predicate with a variable both before and after the list.John
Just google "prolog CLPFD" or "prolog finite domains" and you'll find a lot of goodies. It lets you write relational statements for arithmetic.lurker

1 Answers

0
votes

The following code should do the trick, given that Check_Multiples checks if every element of the list is a multiple of two in an ascending order. I'm guessing that was a condition, otherwise if lists such as [4, 6, 4, 4, 8] were allowed you could just check if every element modulus 2 is equal to 0.

additionsToList([H|T], ResultList) :-
    Check_Multiples([H|T]),
    firstElement(H, First),
    lastElement(T, Last),
    append([First],[Last], Z),
    flatten(Z, ResultList).

firstElement(2, []).
firstElement(First, X) :-
    X is First-2.

lastElement([H|[]], X) :-
    X is H+2.
lastElement([_|T], X) :-
    lastElement(T, X).