
- Prolog - Home
- Prolog - Introduction
- Prolog - Environment Setup
- Prolog - Hello World
- Prolog - Basics
- Prolog - Relations
- Prolog - Data Objects
- Loop & Decision Making
- Conjunctions & Disjunctions
Prolog Operators
- Prolog - Type of Operators
- Prolog - Arithmetic Comparison Operators
- Prolog - Unification Operators
- Prolog - Term Comparision Operators
- Prolog - Arithmetic Operators
- Prolog - Logical Operators
- Prolog - List Operators
- Prolog - Custom Operators
Prolog Lists
- Prolog - Lists
- Prolog - Member of List
- Prolog - Length of List
- Prolog - Concatenating Lists
- Prolog - Appending to a List
- Prolog - Deleting from a List
- Prolog - Inserting into a List
- Prolog - Permutation Operation
- Prolog - Combination Operation
- Prolog - Reverse Items of a List
- Prolog - Shift Items of a List
- Prolog - Check Order of a List
- Prolog - SubSet of a Set
- Prolog - Union of Sets
- Prolog - Intersection of Sets
- Prolog - Even and Odd Length Finding
- Prolog - Divide a List
- Prolog - Find Maximum of a List
- Prolog - Find Minimum of a List
- Prolog - Find Sum of a List
- Prolog - Sorting List using MergeSort
Built-In Predicates
- Prolog - Built-In Predicates
- Prolog - Identifying Terms
- Prolog - Decomposing Structures
- Prolog - Collecting All
- Prolog - Mathematical Predicates
- Prolog - Scientific Predicates
Miscellaneous
- Recursion and Structures
- Prolog - Backtracking
- Prolog - Preventing Backtracking
- Prolog - Different and Not
- Prolog - Inputs and Outputs
- Tree Data Structure (Case Study)
- Prolog - Examples
- Prolog - Basic Programs
- Prolog - Practical Arithmetic Examples
- Prolog - Examples of Cuts
- Towers of Hanoi Problem
- Prolog - Linked Lists
- Monkey and Banana Problem
- Prolog Useful Resources
- Prolog - Quick Guide
- Prolog - Useful Resources
- Prolog - Discussion
Prolog - Lists
In this chapter, we will discuss one of the important concepts in Prolog, The Lists. It is a data structure that can be used in different cases for non-numeric programming. Lists are used to store the atoms as a collection.
In the subsequent sections, we will discuss the following topics −
Representation of lists in Prolog
Basic operations on prolog such as Insert, delete, update, append.
Repositioning operators such as permutation, combination, etc.
Set operations like set union, set intersection, etc.
Representation of Lists
The list is a simple data structure that is widely used in non-numeric programming. List consists of any number of items, for example, red, green, blue, white, dark. It will be represented as, [red, green, blue, white, dark]. The list of elements will be enclosed with square brackets.
A list can be either empty or non-empty. In the first case, the list is simply written as a Prolog atom, []. In the second case, the list consists of two things as given below −
The first item, called the head of the list;
The remaining part of the list, called the tail.
Suppose we have a list like: [red, green, blue, white, dark]. Here the head is red and tail is [green, blue, white, dark]. So the tail is another list.
Now, let us consider we have a list, L = [a, b, c]. If we write Tail = [b, c] then we can also write the list L as L = [ a | Tail]. Here the vertical bar (|) separates the head and tail parts.
So the following list representations are also valid −
[a, b, c] = [a | [b, c] ]
[a, b, c] = [a, b | [c] ]
[a, b, c] = [a, b, c | [ ] ]
For these properties we can define the list as −
A data structure that is either empty or consists of two parts − a head and a tail. The tail itself has to be a list.
Creating a List of three atoms.
Program (list_basics.pl)
:- initialization(main). main :- write([a, b, c]).
Output
| ?- consult('D:/TP Prolog/Sample Codes/list_basics.pl'). compiling D:/TP Prolog/Sample Codes/list_basics.pl for byte code... D:/TP Prolog/Sample Codes/list_basics.pl compiled, 1 lines read - 419 bytes written, 3 ms [a,b,c] yes | ?-
Creating a List of two atoms and a tail of one atom.
Program (list_basics.pl)
:- initialization(main). main :- write([a, b | [c] ]).
Output
| ?- consult('D:/TP Prolog/Sample Codes/list_basics.pl'). compiling D:/TP Prolog/Sample Codes/list_basics.pl for byte code... D:/TP Prolog/Sample Codes/list_basics.pl compiled, 1 lines read - 419 bytes written, 3 ms [a,b,c] yes | ?-
Creating a List of three atoms and an empty tail
Program (list_basics.pl)
:- initialization(main). main :- write([a, b, c | [ ] ]).
Output
| ?- consult('D:/TP Prolog/Sample Codes/list_basics.pl'). compiling D:/TP Prolog/Sample Codes/list_basics.pl for byte code... D:/TP Prolog/Sample Codes/list_basics.pl compiled, 1 lines read - 419 bytes written, 3 ms [a,b,c] yes | ?-