1

I am trying to append list1 to list2 without using append method in prolog. i have searched for it on google but i can't find any solution to do that. Is it possible or not? I have tried it by myself but i can't do it. Here is what i am doing...

apnd(X,[],X). //Stop condition when second list is empty loop will be stopped.
apnd(X,[H|T],R):-//now here i have to append H to X but for this i have to use append method. i don't want to use this. 
//After this i will remove H from second list and loop again for remaining list.

If it's possible to do this without append() method how can i do that?

UPDATE

Reason for not to use append() method

I am engineering student and i am referring past years question papers for my exam preparation and in that they have asked that question. Also after reading that question, i am curious to know this that is it really possible? and if yes then how?. So, i have asked this question here because i didn't find solution or any hint from any of my books or from google.

Community
  • 1
  • 1
Jaydip Kalkani
  • 2,493
  • 6
  • 24
  • 56
  • You mean like `List3 = [List2|List1].`? – G_V May 03 '18 at 07:35
  • No. If `list 1 = [1,2,3]` and `list 2 = [4,5,6]` then i want `list 3 = [1,2,3,4,5,6]`. @G_V – Jaydip Kalkani May 03 '18 at 07:37
  • So `List3 = [List1|List2].` then? You can find explanations on how all this works on [LPN!](http://www.learnprolognow.org/lpnpage.php?pagetype=html). – G_V May 03 '18 at 07:45
  • I very well know how `|` this works and why it's used. I don't want to make List1 Head and List2 tail. I just want to merge both of this lists and Head will be the first element of list1 and remaining list will be the tail. I have already described in comment and question that what i want.@G_V – Jaydip Kalkani May 03 '18 at 07:53
  • 1
    Why don't you want to use that? Please clarify the reasons and add them to your question - such relevant information should not be hidden in the comment section – Nico Haase May 03 '18 at 08:07
  • 1
    I have updated my question. You can check it out. @NicoHaase – Jaydip Kalkani May 03 '18 at 08:13
  • 1
    I can think of two different answers to this, but since you did not post the exact question or even if the exact question is posted my not give enough info to decide what they expect for an answer take a look at. 1) Using [difference list](https://www.cl.cam.ac.uk/teaching/0809/Prolog/Prolog08ML5R2.pdf) 2) Using [listing/1](http://www.swi-prolog.org/pldoc/man?predicate=listing/1) which will give you the code that implements apppend/3 – Guy Coder May 03 '18 at 10:26
  • 2
    There are lots of places you can find simple implementations of `append/3` and just use that source code. For example: https://stackoverflow.com/questions/11539203/how-do-i-append-lists-in-prolog – lurker May 03 '18 at 12:48

1 Answers1

2

There are several posts showing how to write such a predicate on SO. e.g. here or here or here or...

Alternatively, since you are describing a list, you could also opt to use DCGs for this task, e.g.:

list_list_appended(L1,L2,AL) :-  % the appended list
   phrase(appended(L1,L2),AL).   % is described by the DCG appended//2

appended([],[]) -->              % if the two lists are empty
   [].                           % so is the appended list
appended([X|Xs],L) -->           % if the first list starts with X
   [X],                          % X is in the appended list
   appended(Xs,L).               % the same for the tail and the 2nd list
appended([],[X|Xs]) -->          % if the first list is empty but the 2nd isn't
   appended([X|Xs],[]).          % the appended list is the empty list
                                 % appended to the first list

Querying this predicate yields the desired result:

?- list_list_appended([1,2,3],[a,b],AL).
AL = [1, 2, 3, a, b] ;
false.

You can use listing/1 to see how the DCGs is translated into predicates, which gives you yet another version:

?- listing(appended).
appended([], [], A, A).
appended([A|B], C, [A|D], E) :-
        appended(B, C, D, E).
appended([], [A|B], C, D) :-
        appended([A|B], [], C, D).

true.

Note that you can use the predicate in the other direction as well, e.g. Which two lists yield [1,2,3,a,b,c] when appended?:

?- list_list_appended(L1,L2,[1,2,3,a,b,c]).
L1 = [1, 2, 3, a, b, c],
L2 = [] ;
L1 = [1, 2, 3, a, b],
L2 = [c] ;
L1 = [1, 2, 3, a],
L2 = [b, c] ;
L1 = [1, 2, 3],
L2 = [a, b, c] ;
L1 = [1, 2],
L2 = [3, a, b, c] ; 
L1 = [1],
L2 = [2, 3, a, b, c] ;
L1 = [],
L2 = [1, 2, 3, a, b, c] ;
false.
tas
  • 8,100
  • 3
  • 14
  • 22