2

I would like to perform something like:

merge([[[],[],[],[t1]],[[],[],[],[t2,t3]]], X).

where X would return as: [[],[],[],[t1,t2,t3]].

But I have tried everything to my prolog knowledge and came up with nothing.

Any hints?

Imagine it as:

Computer(
    Tasklist1(
            core1[sometasks],core2[sometasks],...,coreX(sometasks)), 
    ...
    TasklistX(
            core1[sometasks],core2[sometasks],...,coreX(sometasks))
)

so the tasklist after tasklist1 needs to be scheduled on the same cores, after the tasks of tasklist1.

voluminat0
  • 866
  • 2
  • 11
  • 21

2 Answers2

1

I thought it could be easier...

merge(L, R) :-
    maplist(length_list(N), L),
    findall(S, (
        between(1,N,I),
        findall(Zs, (
            member(Z,L),
            nth1(I,Z,Zs)), T),
        append(T, S)), R).

length_list(Len, L) :- length(L, Len).
CapelliC
  • 59,646
  • 5
  • 47
  • 90
1

It's not totally clear what the limits of the problem may be. But here is a solution which assumes you may have more than two inner list-of-lists, and the count of the innermost lists might vary.

merge2(L, [], L) :- L \= [].
merge2([], L, L).
merge2([LH1|LT1], [LH2|LT2], [LH3|LT3]) :-
    append(LH1, LH2, LH3),
    merge2(LT1, LT2, LT3).

merge([L], L).
merge([H1,H2|T], R) :-
    merge2(H1, H2, H),
    merge([H|T], R).

So,

| ?- merge([[[],[],[],[t1]],[[],[],[],[t2,t3]]], L).

L = [[],[],[],[t1,t2,t3]] ? ;

no
| ?- merge([[[1],[2],[3]], [[4],[5],[6]],[[a],[b],[c,d]]], L).

L = [[1,4,a],[2,5,b],[3,6,c,d]] ? a

no
| ?- merge([[[1],[2],[3]], [[5],[6]],[[a],[b],[c,d]]], L).

L = [[1,5,a],[2,6,b],[3,c,d]] ? ;

(1 ms) no
| ?-

If you want to restrict the innermost list counts to be the same, you can replace merge2 with maplist, and the merge predicate simply becomes:

merge([L], L).
merge([H1,H2|T], R) :-
    maplist(append, H1, H2, H),
    merge([H|T], R).
lurker
  • 56,987
  • 9
  • 69
  • 103
  • Thanks! This is what I was looking for. sorry for the late reply – voluminat0 Dec 30 '14 at 09:41
  • @ValentijnSpruyt no worries. You can also simplify the solution using `maplist` if inner list counts are the same, per my edited answer. – lurker Dec 30 '14 at 12:45