0
votes

I'm currently learning how to code in Prolog. I have been trying to connect 2 lists to create a new list such that the first element of the first list will be followed by the first element of the second which will be followed by the second element of the first list and so on. Here is an example of what I'm talking about. Can someone explain to me what I'm doing wrong? It maybe a very simple error that I'm not catching because I'm still learning.

First list: (q, r, s) second list: (l, m, n)

Final list: (q, l, r, m, s, n)

1
this is know as zip/3CapelliC

1 Answers

1
votes

You need to use the head of each rule to specify when it applies. For example:

connectLists([],L,L).
connectLists(L,[],L).
connectLists([H1|T1],[H2|T2],[H1,H2|T]) :-
    connectLists(T1,T2,T).