MadPat
MadPat

Reputation: 53

Split list of two nested lists in two separated lists - Prolog

I've done my research already and I can't find anything similar. I need to split a list of two lists in two separated lists, for example: [ [[1,2],[2],[3]], [[3], [2,3], [5]] ] -> results in [[1,2],[2],[3]] and [[3], [2,3], [5]] .

I tried this but I can't seem to find where I need to change the code:

split([],[],[]).
split([H|T],X,Y):-
   X = H,
   Y = T.

Upvotes: 1

Views: 88

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476564

You can just unpack the list in two parameters:

split([L1, L2], L1, L2).

Here we thus unify the first parameter with [L1, L2] such that the list of length two is unified in that way that L1 unifies with the first item (sublist), and L2 with the second item (sublist).

Upvotes: 1

Related Questions