INFS3450 – RMU C&IS

Logic Programming –Prolog List Structures

The SWI Prolog documentation (see http://www.swi-prolog.org/ for details) identifies the following SWI Prolog built in predicates relating to list checking and manipulation:

The Prolog list data structure is represented enclosed in square brackets [ ] or [H|T] with list elements separated by commas.

Prolog list may have duplicate elements, but when a list data structure is used to represent a set, there are no duplicates.

Every list has a head (the first element in the list) and a tail (the list consisting of the remaining elements of the list).

The empty list is represented by: [ ]. Different instantiation patterns than those shown are possible. See SWI-Prolog documentation.

A nonempty list could be represented as: [1, 2, 3, 4, 5].

  • append/3; append(list1 in, list2 in, merged list out).

1 ?- append([pa,ny,oh,wv],[nh,vt,ct,ma,me],X).

X = [pa, ny, oh, wv, nh, vt, ct, ma, me]

Yes

  • is_list/1
    Example 1 (succeeds):

2 ?- is_list([pa,ny,oh,pa,nj]).

Yes

  • member/2; member(element in, list out)
    Example 1 (succeeds):

3 ?- member(ct,[de,ct,ny,me]).

Yes

Example 2 (fails):

4 ?- member(tx,[de,ct,oh,pa]).

No

  • member/2; member(element out, list in)
    Example 1:

5 ?- member(X,[pa,ny,de,wv]).

X = pa ;

X = ny ;

X = de ;

X = wv ;

No

Example 2: (with list elements)

6 ?- member([houston,X],[[austin,tx],[erie,pa],[houston,tx],[scranton,pa],[houston,pa]]).

X = tx ;

X = pa ;

No

  • Example 3: (Generates lists with element in in different positions:

7 ?- member(x, X).

X = [x|_G294] ;

X = [_G293, x|_G297] ;

X = [_G293, _G296, x|_G300] ;

X = [_G293, _G296, _G299, x|_G303]

Yes

  • [H|T] list pattern,
    Example 1:

8 ?- [H|T] = [pa,ny,wv,md].

H = pa

T = [ny, wv, md]

Yes

Example 2:

9 ?- [Head|Tail] = [pgh,erie,hbg,phila].

Head = pgh

Tail = [erie, hbg, phila]

Yes

  • sort/2; sort(list in, sorted list out)

10 ?- sort([pa,ny,nj,de,oh,tx,nm,ok,la],X).

X = [de, la, nj, nm, ny, oh, ok, pa, tx]

  • length(list in, value in)
    Example 1 (succeeds):

11 ?- length([de,ct,oh,pa],4).

Yes

Example 2 (fails):

12 ?- length([de,ct,oh,pa],5).

No

  • merge(list1 in, list2 in, merged list out).

13 ?- merge([pa,md,ny,nj,de],[tx,nm,ok,la],X).

X = [pa, md, ny, nj, de, tx, nm, ok, la]

  • nth0/3; nth0(index # in, list in, element out) Note: leftmost element is element #0

14 ?- nth0(3,[pa,ny,de,nh],X).

X = nh

Yes

  • nth1/3; nth1(index # in, list in, element out) Note: leftmost element is element #1

15 ?- nth1(3,[pa,ny,de,nh],X).

X = de

Yes

  • last/2; last(list in, last element out] Note: this predicate gives the tail of the list.

16 ?- last([pa,ny,de,nh,vt],X).

X = vt

Yes

  • reverse/2; reverse(list in, reversed list out)

17 ?- reverse([pa,ny,nj,de,me,nh,ct],X).

X = [ct, nh, me, de, nj, ny, pa]

Yes

  • permutation/2; permutation(list in, permutation of list out)

18 ?- permutation([pa,ny,wv],X).

X = [pa, ny, wv] ;

X = [ny, pa, wv] ;

X = [ny, wv, pa] ;

X = [pa, wv, ny] ;

X = [wv, pa, ny] ;

X = [wv, ny, pa] ;

No

  • numlist(low num in, high num in, list out).

19 ?- numlist(2,14,X).

X = [2, 3, 4, 5, 6, 7, 8, 9, 10|...]

Yes

  • nextto/3; nextto(element1 in, element2 in, list in). Note: checks whether element2 in follows element1 in in list in
    Example 1 (succeeds):

20 ?- nextto(pa,ny,[ct,me,pa,ny,nj,de]).

Yes

Example 2 (fails):

21 ?- nextto(pa,nj,[ct,me,pa,ny,nj,de]).

No

  • sumlist([number list in], X).

22 ?- sumlist([3,4,7,2],X).

X = 16

Yes

  • delete/3; delete(list in, element in, list minus occurrence(s) of element in)

23 ?- delete([pa,ny,oh,pa,nj,ny],pa,X).

X = [ny, oh, nj, ny]

Yes

  • flatten/2; flatten(list* in, flattened list out) Note: for list in, list has elements which are lists (nested list structure)

24 ?- flatten([pa,[pgh,phila,hbg],ny, [nyc,alb]],X).

X = [pa, pgh, phila, hbg, ny, nyc, alb]

Yes

Updated Feb. 20, 06

SWI Prolog licensed under GNU Public License; Documentation, Jan Wielemaker, HCS group, University of Amsterdam
Contact for this page:

Valerie J. Harvey, RT(R), PhD, C&IS, RMU