0% found this document useful (0 votes)
2 views

Ch2_1 CS3032

The document provides an introduction to lists in Prolog, explaining their structure and usage for organizing related data. It includes examples of basic operations such as inserting elements at the beginning and end of a list, along with exercises for further practice. The document emphasizes the significance of the head and tail in list manipulation.

Uploaded by

BT VITHU
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Ch2_1 CS3032

The document provides an introduction to lists in Prolog, explaining their structure and usage for organizing related data. It includes examples of basic operations such as inserting elements at the beginning and end of a list, along with exercises for further practice. The document emphasizes the significance of the head and tail in list manipulation.

Uploaded by

BT VITHU
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Logic Programming

CS 3032

3rd YEAR 1st SEMESTER


August, 2021
- CHAPTER 02 –
Lists & Arithmetic in Prolog

2
Introduction to Lists

▪ A list in Prolog is a collection of term, which is essential for clustering


items together or for analyzing with large volumes of related data.

▪ Prolog lists are standard and build-in structures.

▪ Lists can be used to represent,


o Sets / Multi-sets
o Sequences
o Complex structure

3
• Prolog list structure for some elements such as { a, b, c, d, e, f } can be written as :
[ a, b, c, d, e, f ]

• Direct access is only to the first element in the list called, Head, while rest forms
the list Tail.

[ Head | Tail ]

One single List


element

Partition Symbol
Lists in Prolog: Example
/* The part which is considered before the
vertical bar (|), inside the square brackets is
the head of given list elements

It always take an element as a head part


*/

/* list of k elements can be matched directly


against elements*/

/* It is possible to divide head part into


several single parts*/
Lists in Prolog: Example

Write down the explanation


for these given queries
Basic Programs in Lists: Example 1

1. Inserting an element at the first position of the given list.


Basic Programs in Lists: Example 1 (contd.)

/* condition for if empty list is given*/

/* condition for inserting an element at the first


position*/

• Name of the operation is insert_first.


• First the list should be given (L).
• Then the element which you want to insert should be given (X).
• After that, displaying format for the list should be given. (X should me in the front according to
the task, so just make it as Head for the list).
• Note that, you don’t need to write in this format only. It depends on your imagination. You can
do it in any format.
2. Write a prolog program to insert an element at the last
position of the given list

9
Basic Programs in Lists: Example 2

2. Inserting an element at the last position of the given list.


Basic Programs in Lists: Example 2 (contd.)

• If you want to find out how a prolog


program works, simply you can use the
command: trace.

• Then it will ask you which command you


are going to trace. So, you can enter the
command which you want to trace.
Exercises
1. Write a Prolog program to find the maximum of given two numbers. Consider the
following format to get an output for your program.

12

You might also like