[ library(lists) | Reference Manual | Alphabetic Index ]

splice(?Odds, ?Evens, ?List)

Merge two lists by interleaving the elements
Odds
List or variable
Evens
List or variable
List
Variable or list

Description

Create a new list by alternating elements from two input lists, starting with the first. When one input list is longer, its extra elements form the tail of the result list.

The reverse mode splice(-,-,+) is nondeterministic, and the most balanced solution(s) will be found first.

Modes and Determinism

Examples

?- splice([1,2,3], [a,b,c], X).
X = [1, a, 2, b, 3, c]
Yes (0.00s cpu)

?- splice([1,2,3], [a,b,c,d,e], X).               
X = [1, a, 2, b, 3, c, d, e]
Yes (0.00s cpu)

?- splice(A, B, [1,a,2,b,3,c]).
A = [1, 2, 3]
B = [a, b, c]
More (0.00s cpu) ? ;
A = [1, 2, 3, c]
B = [a, b]
More (0.00s cpu) ? ;
A = [1, 2]
B = [a, b, 3, c]
More (0.00s cpu) ? ;
A = [1, 2, b, 3, c]
B = [a]
More (0.00s cpu) ? ;
A = [1]
B = [a, 2, b, 3, c]
More (0.00s cpu) ? ;
A = [1, a, 2, b, 3, c]
B = []
More (0.00s cpu) ? ;
A = []
B = [1, a, 2, b, 3, c]
Yes (0.00s cpu)

See Also

merge / 3