Happy Lad
Happy Lad

Reputation: 17

Prolog, create a predicate that grab 3 diagonal values from 3 lists

Hi so I have 3 lists = [1,2,3], [4,5,6], [7,8,9]

I want to create a predicate that create 2 list using the diagonal values from the 3 list.

Here is some example of using said predicate:

?- diagonal([1,2,3], [4,5,6], [7,8,9], D1, D2).
D1 = [1,5,9],
D2 = [3,5,7].

D1 = [3,5,7],
D2 = [1,5,9].

Right now I can only think of hard-coding it but it would only give back only 1 answer instead of letting me press space and get the second one.

Anything helps.

Upvotes: 0

Views: 83

Answers (1)

slago
slago

Reputation: 5519

To solve this problem, you only need to use unification.

diagonal([A,_,B], [_,C,_], [D,_,E], [A,C,E], [B,C,D]).

Query:

?- diagonal([1,2,3], [4,5,6], [7,8,9], D1, D2).
D1 = [1, 5, 9],
D2 = [3, 5, 7].

Upvotes: 1

Related Questions