Friday 15 February 2019

Prolog: Append elements of a list


Prolog provides append rule, to append the elements of a list.

1 ?- append([], [], X).
X = [].

2 ?- append([], [1, 2], X).
X = [1, 2].

3 ?- append([3, 4], [1, 2], X).
X = [3, 4, 1, 2].

4 ?- append([3, 4], [1, 2, 3], X).
X = [3, 4, 1, 2, 3].


You can even use the append method in other way.

5 ?- append(X, [1, 2, 3], [3, 4, 1, 2, 3]).
X = [3, 4] .

6 ?- append([3, X], [1, 2, 3], [3, 4, 1, 2, 3]).
X = 4.

7 ?- append([3, X], [Y, Z, 3], [3, 4, 1, 2, 3]).
X = 4,
Y = 1,
Z = 2.


Previous                                                 Next                                                 Home

No comments:

Post a Comment