Showing posts with label prolog list. Show all posts
Showing posts with label prolog list. Show all posts

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

Prolog: Check whether given element is list or not


Below snippet checks whether given element is list or not.

listDemo.pl
isList([]).
isList([_|Tail]) :- isList(Tail).

1 ?- consult(listDemo).
true.

2 ?- isList([]).
true.

3 ?- isList([2]).
true.

4 ?- isList([2, 3, 5]).
true.



Previous                                                 Next                                                 Home