Derrick Park
Derrick Park

Reputation: 67

How do I get every consecutive elements from a list in Prolog

Let say I have a list [[a,b,c],[d,e,f],[g,h,i]]

I want to get every consecutive element and put it in another predicate.

func(a,b).
func(b,c).

func(d,e).
func(e,f).

func(g,h).
func(h,i).

I already wrote the predicate I want to put in, but I'm having a hard time getting two elements from the list of lists.

Upvotes: 0

Views: 803

Answers (1)

joel76
joel76

Reputation: 5615

You can try :

consecutive(L, R) :-
    maplist(create_func, L, RT),
    flatten(RT, R).

create_func([A,B], [func(A,B)]) :- !.

create_func([A,B | T], [func(A,B) | R]) :-
    create_func([B | T], R).

Upvotes: 1

Related Questions