Let In = [1,2,-1,4,5,-1,-1,8,9,10]
and Replace = [3,6,7,8,9,10]
, write merge(In, Replace, Out)
such that Out = [1,2,3,4,5,6,7,8,9,10]
where the predicate replaces -1 elements in turn from the Replace
stack.
This is easy to imagine as a recursion of the form merge([X|T1], [Y|T2], Acc, Out)
, but how would you write this down declaratively without resorting to recursion? I.e. how would you specify the constraints for this solution and let Prolog solve it, rather than expressing the algorithm directly?