Bilal Yasar
Bilal Yasar

Reputation: 967

How could i solve this with Prolog?

i have list in prolog for example L=[a,b,c,d,e,f] , i want to convert this list to new list that has this type L2=[[a,b],[c,d],[e,f]], in other words, i want to convert every two consecutive element in the list to new list.

Upvotes: 0

Views: 50

Answers (1)

CapelliC
CapelliC

Reputation: 60014

the plain Prolog, tailored on your requirements:

'convert every two consecutive element'([], []). % base case
'convert every two consecutive element'([A,B|R], [[A,B]|S]) :-
  'convert every two consecutive element'(R, S).

The list length must be even for this to succeed, otherwise you will need to refine the base case.

Upvotes: 3

Related Questions