Week 2: Notes

terms

All data in Prolog is represented as terms. Here are the kinds of terms we have learned about so far:

list predicates

Here are the predicates that we wrote in the lecture.

% is_empty(L) is true if L is the empty list.
is_empty([]).

% non_empty(L) is true if L is a non-empty list.
non_empty([_ | _]).

% first_elem(X, L) is true if X is the first element of L.
first_elem(X, [X | _]).

% last_elem(X, L) is true if X is the last element of L.

last_elem(X, [X]).
last_elem(X, [_ | L]) :- last_elem(X, L).

% member(X, L) is true if X is any member of the list L.

member(X, [X | _]).
member(X, [_ | L]) :- member(X, L).

% append(L, M, N) is true if we can append L and M to make N.
% In other words, it's true if L + M = N, where + means list concatenation.

append([], L, L).
append([X | L], M, [X | N]) :- append(L, M, N).