2

I'm working on a simple Prolog example I saw in one of my classes, working with and re-implementing 'append'. This is what I have (from the class):

append1([],L,L).
append1([X|L1],L2,[X|L3]) :-
    append1(L1,L2,L3).

When I do this, results in false:

append1([a,b],[c,d],L).
false.

But when I do this (with the built-in append), results in true:

append([a,b],[c,d],L).
true.

Why is there a difference?

And why do both of these following append examples even result in 'true'? For some reason I expected to see a list of values for the Ls ...

append(L1,L2,[a,b,c]).
true.

append([a,b],[c,d],L).
true.
false
  • 10,264
  • 13
  • 101
  • 209
  • You haven't specified the relationship between `L3` and the inputs `L1`, and `L2`. – Barmar Apr 04 '13 at 02:00
  • @Barmar Sorry, I left out a line in my question. Fixed it. Any other help is useful! –  Apr 04 '13 at 02:03
  • no, everything works as expected. – Will Ness Apr 04 '13 at 02:11
  • @WillNess I guess what I need is to learn is what append is actually doing. I'm missing something. –  Apr 04 '13 at 02:16
  • I can offer you [this](http://stackoverflow.com/questions/11539203/how-do-i-append-lists-in-prolog/11551001#11551001). Do notice that you have a typo in `append1` - you call `append` inside it, instead of recursively calling `append1` itself. – Will Ness Apr 04 '13 at 02:18
  • 1
    You changed the definition of append/3. In case of doubt, do a `listing(appendl)` to see what Prolog actually got from you. – false Apr 04 '13 at 13:50

1 Answers1

1

Works fine:

Welcome to SWI-Prolog (Multi-threaded, 64 bits, Version 7.1.33)
Copyright (c) 1990-2015 University of Amsterdam, VU Amsterdam

?- [user].
append1([],L,L).
append1([X|L1],L2,[X|L3]) :-
   append1(L1,L2,L3).
^D
true.

?- append1([a,b],[c,d],L).
L = [a, b, c, d].

Note: The ^D is the ctrl character you need to press to get out of [user] mode. You don't have to be in [user] mode to pose a query. Could be the error you were running into.

Bye