

Bad naming! Bad, bad naming!
Instead of append
, this predicate should have been called concatenated/3 and the parameters called
concatenated(PrefixList,SuffixList,PrefixSuffixList)
because PrefixSuffixList is the result of concatenating PrefixList and SuffixList, not of appending SuffixList to PrefixList (which would of course yield for example [a,b,c,d,SuffixList]
if PrefixList were [a,b,c,d]
).
Additionally, the active verb form implies a function, whereas the predicate expresses a relation between the three arguments, it is not (only) a function.
Don't use append excessively
From the description of flatten/2 :
"Ending up needing flatten/2 often indicates, like append/3 for appending two lists, a bad design."
...because one would generally append elements one-by-one to a "difference list", or prepend elements one-by-one to a proper list instead of concatenating large lists in one step.
append does nothing surprising
It's not optimized in any way.
Clicking on the "source code" button on the top right (see above) reveals that it does exactly what you would do when coding append by hand:
- recurse down the backbone of
List1
until [] has been reached - while doing so copy
List1
element by element into a new list growing at the end - until the "empty cell" that is at the end of the backbone of the copied list can be unified with
List2
, creating a concatenated list with the contents ofList1
andList2
.
Examples
An unbound variable at argument position 3: concatenation
?- append([1,2],[a,b],X). X = [1, 2, a, b].
An unbound variable at argument position 2: shaving off a prefix from the 3rd argument
?- append([1,2],X,[1,2,a,b]). X = [a, b].
An unbound variable at position 1: shaving off a suffix from the 3rd argument
append/3 is unsure whether there might be a 2nd solution (this might be optimized)
?- append(X,[a,b],[1,2,a,b]). X = [1, 2] ; <--- maybe more solutions? false. <--- actually not
Unbound variables on position 1 and 2? Guessing a prefix and a suffix.
There are multiple answers! (below, made more legible than what is printed in reality):
?- append(X,Y,[1,2,a,b]). X = [], Y = [1, 2, a, b] ; <--- maybe more solutions? X = [1], Y = [2, a, b] ; <--- yes... maybe more solutions? X = [1, 2], Y = [a, b] ; <--- yes... maybe more solutions? X = [1, 2, a], Y = [b] ; <--- yes... maybe more solutions? X = [1, 2, a, b], Y = [] ; <--- yes... maybe more solutions? false. <--- actually not
Append can handle other things than lists.
I'm not sure whether this is intended, Maybe there should be an exception? On the other hand, flexibility in all things is an asset.
?- append([a,b,c,d],foo,[a,b,c,d|foo]). true.