0% found this document useful (0 votes)
33 views25 pages

PCPF Lab Manual-Seit

Experiments of pcpf in second year of engineering , all the covered sets of experiments

Uploaded by

rkprasad882
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views25 pages

PCPF Lab Manual-Seit

Experiments of pcpf in second year of engineering , all the covered sets of experiments

Uploaded by

rkprasad882
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

SARASWATI EDUCATION SOCIETY

YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING AND TECHNOLOGY


DR.N.Y.TASGAONKAR TECHNICAL EDUCATION CAMPUS, CHANDHAI AT.POST:-NASARAPUR, TAL: - KARJAT, DIST: - RAIGAD

Department of INFORMATION TECHNOLOGY Engineering

LAB MANUAL
Academic year (2024-25)

Year: SE(IT) Semester: III

SUB-PCPF

Prepared By

PROF.PRASHALI R KAMBLE
SARASWATI EDUCATION SOCIETY
YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING AND TECHNOLOGY
DR.N.Y.TASGAONKAR TECHNICAL EDUCATION CAMPUS, CHANDHAI AT.POST:-NASARAPUR, TAL: - KARJAT, DIST: - RAIGAD

LIST OF PRACTICAL

S.No Name of the Experiment Page No

1 To demonstrate concept of encapsulation

2 Write a program to demonstrate concept of inheritance in C++

3 Write a program to initialize the information of a car with the


argument passed to it at

4 Write a program to execute operators in Haskell

5 Write a program for lists in Haskell

6 Write a program to understand Exception handling

7 Write a program to Garbage collection

8 Write a program to understand thread management and


synchronization

9 To install SWI Prolog

10 To understand basics of SWI PROLOG

11 To understand basic concepts in SWI PROLOG

12 To study List in SWI PROLOG


SARASWATI EDUCATION SOCIETY
YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING AND TECHNOLOGY
DR.N.Y.TASGAONKAR TECHNICAL EDUCATION CAMPUS, CHANDHAI AT.POST:-NASARAPUR, TAL: - KARJAT, DIST: - RAIGAD

EXPERIMENT NO: 1

Aim: To demonstrate concept of encapsulation

Requirements: turbo C

Objective: To Understand the concept of data abstraction and and encapsulation.

Theory: It is a process of wrapping code and data together into a single unit, for example, a
capsule which is mixed of several medicines.
For example 1: School bag is one of the most real examples of Encapsulation. School bag can keep
our books, pens, etc. Real-time
Example 2: When you log into your email accounts such as Gmail, Yahoo Mail, or Rediff mail,
there is
a lot of internal processes taking place in the backend and you have no control over it.

Outcome: Implement Object Oriented concepts in C++.

Code:
#include<iostream.h>
#include<conio.h>
class sum
{
private: int a,b,c;
public: void add()
{
clrscr();
cout<<"enter two numbers" ;
cin>>a>>b;
c=a+b;
cout<<"sum="<<c;
}
};
void main()
{
sum s;
s.add();
getch();
}

output
enter two numbers 5 6
sum= 11

Conclusion: Thus we studied concept of encapsulation


SARASWATI EDUCATION SOCIETY
YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING AND TECHNOLOGY
DR.N.Y.TASGAONKAR TECHNICAL EDUCATION CAMPUS, CHANDHAI AT.POST:-NASARAPUR, TAL: - KARJAT, DIST: - RAIGAD

EXPERIMENT NO: 2

TITLE: Inheritance in C++

Aim: Write a program to demonstrate concept of inheritance in C++

Requirements: Turbo C

Objective: Understand data object orientation concept

Theory: It allows us to create a new class (derived class) from an existing class (base class). The
derived class inherits the features from the base class and can have additional features of its
own.
Example1 class Animal { // eat() function // sleep() function };
class Dog : public Animal { // bark() function };

Outcome: Implement Object Oriented concepts in C++

Code:
using namespace std;
class Parent
{p
ublic:
int id_p;
};
class Child : public Parent {
public:
int id_c;
};
int main()
{
Child obj1;
obj1.id_c = 7;
obj1.id_p = 91;
cout << "Child id is: " << obj1.id_c << '\n';
cout << "Parent id is: " << obj1.id_p << '\n';
return 0;
}
output
Child id is: 7
Parent id is: 91

Conclusion: Thus we studied concept of inheritance in C++


SARASWATI EDUCATION SOCIETY
YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING AND TECHNOLOGY
DR.N.Y.TASGAONKAR TECHNICAL EDUCATION CAMPUS, CHANDHAI AT.POST:-NASARAPUR, TAL: - KARJAT, DIST: - RAIGAD

EXPERIMENT NO: 3
TITLE: Initialization and Finalization

Aim: Write a program to initialize the information of a car with the argument passed to
it at
compile time.

Requirements: Turbo C
Objective: Understand data object orientation concept

Theory:
Constructors are used to initialize a value to the variable either at run time or at compile
time.
A constructor is a special type of member function that is called automatically when an
object is created.
A constructor has the same name as that of the class and it does not have a return type.
Types of constructor are 1.Default Constructor 2.Parameterized Constructor 3.Copy
Constructor

Outcome: Implement Object Oriented concepts in C++

Code:
class Car // The class
{
public: // Access specifier
string brand; // Attribute
string model; // Attribute
int year; // Attribute
Car(string x, string y, int z) { // Constructor with parameters
{ brand = x;
model = y;
year = z;
}};
int main()
{
Car Obj1("BMW", "X5", 1999);
Car Obj2("Ford", "Mustang", 1969);
cout << Obj1.brand << " " << Obj1.model << " " <<
Obj1.year << "\n";
cout << Obj2.brand << " " << Obj2.model << " " <<
Obj2.year << "\n";
return 0;
}
output
BMW X5 1999
Ford Mustang 1969
Conclusion: Thus we studied concept of Initialization and Finalization
SARASWATI EDUCATION SOCIETY
YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING AND TECHNOLOGY
DR.N.Y.TASGAONKAR TECHNICAL EDUCATION CAMPUS, CHANDHAI AT.POST:-NASARAPUR, TAL: - KARJAT, DIST: - RAIGAD

EXPERIMENT NO: 4

TITLE: Haskell
Aim: Write a program to execute operators in Haskell

Requirements: Compiler on Haskell

Objective: Design and implement declarative programs in functional and logic


programming language.

Theory: Haskell is a widely used purely functional language. Functional


programming is based on mathematical functions. Programs in Haskell are always
written as mathematical functions

Outcome: Design and Develop solution using functional and logic programming.
SARASWATI EDUCATION SOCIETY
YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING AND TECHNOLOGY
DR.N.Y.TASGAONKAR TECHNICAL EDUCATION CAMPUS, CHANDHAI AT.POST:-NASARAPUR, TAL: - KARJAT, DIST: - RAIGAD
SARASWATI EDUCATION SOCIETY
YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING AND TECHNOLOGY
DR.N.Y.TASGAONKAR TECHNICAL EDUCATION CAMPUS, CHANDHAI AT.POST:-NASARAPUR, TAL: - KARJAT, DIST: - RAIGAD

Basic pre-defined function operations in Haskell

Succ: It gives the next number of current number


SARASWATI EDUCATION SOCIETY
YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING AND TECHNOLOGY
DR.N.Y.TASGAONKAR TECHNICAL EDUCATION CAMPUS, CHANDHAI AT.POST:-NASARAPUR, TAL: - KARJAT, DIST: - RAIGAD

Min/max: It gives minimum and maximum value from given numbers.

Conclusion: Thus we studied concept of Haskell and operators in Haskell


SARASWATI EDUCATION SOCIETY
YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING AND TECHNOLOGY
DR.N.Y.TASGAONKAR TECHNICAL EDUCATION CAMPUS, CHANDHAI AT.POST:-NASARAPUR, TAL: - KARJAT, DIST: - RAIGAD

EXPERIMENT NO: 5
TITLE: LIST

Aim: Write a program for lists in Haskell.

Requirements: Any text editor to be able to edit Haskell code and Glasgow Haskell Compiler 8.0+
version.
Objective: Design and implement declarative programs in functional and logic programming
languages
Theory: It stores several elements of the same type. That means that we can have a list of
integers or a list of characters but we can't have a list that has a few integers and then a few
characters.
Outcome: Design and Develop solution using functional and logic programming.

1.a. Now u create a list of 5 Float numbers with name x and print its values

Prelude> let x= [4.5,6.0,5.0,4.6,6.8778]


Prelude> x

[4.5, 6.0, 5.0, 4.6, 6.8778]

1.b. Now u creates a list of 3 chars with name y and print its values

Prelude> let y=['f','d','f']


Prelude> y
"fdf"

1.c. Now u create a list of 3 Strings with name myname that contain your first name, middle
name and last name and print its values

Prelude> myname=["Anup","Shrikant","Kunte"]
Prelude> myname
["Anup","Shrikant","Kunte"]
CPCL Lab Manual
PHCET- S.E. (IT)Page 2

2. Execute these command and write down output (Learn use of ++ (concatination
operator))
2.a. Prelude>[1,2,3,4] ++ [9,10,11,12]

Output : [1,2,3,4,9,10,11,12]

2.b. Prelude> "hello"++" "++"world!"

Output: "hello world!"


SARASWATI EDUCATION SOCIETY
YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING AND TECHNOLOGY
DR.N.Y.TASGAONKAR TECHNICAL EDUCATION CAMPUS, CHANDHAI AT.POST:-NASARAPUR, TAL: - KARJAT, DIST: - RAIGAD

2.c. Prelude> [‘w’,’h’] ++ [‘a’,’t’]

Output : "what"
3. Learn how to work with cons (:) operator
3.a. Prelude>'A':" SMALL CAT"

Output: "A SMALL CAT"


3.b. Prelude>1:[4,6]

Output: [1,4,6]
3.c. Prelude>1:4:6:[]

Output : [1,4,6]
3.d. Prelude>1:4:6

Output: Error

4. Indexing operator (!!) Note output o below commands and answer following:

4.a Prelude > [9.4,33.2,96.2,11.2,23.25]!!3 Output : 11.2

4.b Prelude > [9.4,33.2,96.2,11.2,23.25]!!0 Output : 9.4

5. Now we will make a table of basic functions on list you need to fill in missing entries:
Sr. Name Example Output Type signature

1 head head [4,2,5,6] 4 head :: [a] -> a


2 tail tail [5,4,3,2,1] [4,3,2,1]
3 last last [5,4,3,2,1] 1
4 init init [4,2,5,6] [4,2,5]
5 length length “hi there” 8
6 null null [] True
7 reverse reverse [5,6,3] [3,6,5]
8 take take 3 [5,6,2,3,4,3] [5,6,2]
9 drop drop 2 [4,5,2,2,4] [2,2,4]
10 elem 4 `elem` [3,4,5,6] True
11 maximum minimum [8,4,2,1,5] 1
12 minimum maximum [1,9,2,3,4] 9

Conclusion: Thus we studied concept of Haskell and lists in Haskell.


SARASWATI EDUCATION SOCIETY
YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING AND TECHNOLOGY
DR.N.Y.TASGAONKAR TECHNICAL EDUCATION CAMPUS, CHANDHAI AT.POST:-NASARAPUR, TAL: - KARJAT, DIST: - RAIGAD

EXPERIMENT NO: 6

TITLE: Exception Handling

Aim: Write a program to understand Exception handling

Requirements: jdk 1.8 or higher,eclipse

Objective: Understand run time program management

Theory: Exception Handling:


An exception is an event that disrupts the normal execution of a program. Java uses
exceptions to
handle errors and other abnormal events during the execution of the program. The master
class for thisis class ’throwable’ in Java.

When we discuss exceptions, we must understand that they are different from Errors.
Errors are
impossible to recover while exceptions are recoverable. Errors only occur at run-time and
are caused bythe Java run time environment, while exceptions can occur at compile time
as well and the cause is the application itself, not the Java compiler.

In Java, exceptions that occur at compile time are called checked exceptions and run-
time exceptionsare called unchecked exceptions.
A basic example of an exception is shown
below:class SampleException{

public static void main(String

args[]){try {

//code that raise exception

catch (Exception e) {

// rest of the program }

}
}

The code that needs to be executed in case an exception occurs should be placed within the
catch
SARASWATI EDUCATION SOCIETY
YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING AND TECHNOLOGY
DR.N.Y.TASGAONKAR TECHNICAL EDUCATION CAMPUS, CHANDHAI AT.POST:-NASARAPUR, TAL: - KARJAT, DIST: - RAIGAD

block. In the finally block, you need to put in code that is executed irrespective of whether
an exceptionis thrown or not.

1. try : A try block is where you will put the code that may raise exceptions.
2. catch: The catch block is used to handle a given exception.
3. finally: The keyword ‘finally’ is used to define the code block that must be
executed irrespectiveof the occurrence of an exception.

Outcome: Understand the need and use of exception handelling.

Program: using JAVA


public class JavaExceptionExample
{
public static void main(String args[])
{
Try
{
//code that may raise exception
int data=100/0;
}
catch(ArithmeticException e)
{
System.out.println(e);

//rest code of the program


System.out.println("rest of the code...");
}
}
SARASWATI EDUCATION SOCIETY
YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING AND TECHNOLOGY
DR.N.Y.TASGAONKAR TECHNICAL EDUCATION CAMPUS, CHANDHAI AT.POST:-NASARAPUR, TAL: - KARJAT, DIST: - RAIGAD

OUTPUT
Program: using
C++ using
namespace std;
void main()
{
Try
{
//code that may raise
exceptionint data=100/0;
}
catch(ArithmeticException e)
{
cout(e);
}
//rest code of the
programcout("rest of
the code...");
}

Conclusion: Thus we studied Exception handling and Garbage collection using JAVA
SARASWATI EDUCATION SOCIETY
YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING AND TECHNOLOGY
DR.N.Y.TASGAONKAR TECHNICAL EDUCATION CAMPUS, CHANDHAI AT.POST:-NASARAPUR, TAL: - KARJAT, DIST: - RAIGAD

EXPERIMENT NO: 7
TITLE: Garbage collection

Aim: Write a program to Garbage collection,

Requirements: jdk 1.8 or higher,eclipse

Objective: Understand run time program management

Theory: Java Garbage Collection

In java, garbage means unreferenced objects.Garbage Collection is process of


reclaiming the runtime unused memory automatically. In other words, it is a way to
destroy the unused objects.To do so, we were using free() function in C language and
delete() in C++. But, in java it is performed automatically. So, java provides better
memory management.

Advantage of Garbage Collection

It makes java memory efficient because garbage collector removes the


unreferenced objectsfrom heap memory.
It is automatically done by the garbage collector(a part of JVM) so we don't need to
make extraefforts.

How can an object be unreferenced?

By nulling the reference


By assigning a reference to another
By anonymous object etc.

Outcome: Understand the need and use of garbage collection in java.

Program for garbage collector


public class TestGarbage1
{
public void finalize()
{System.out.println("object is garbage collected");
}
public static void main(String args[])
{
TestGarbage1 s1=new
TestGarbage1(); TestGarbage1
s2=new TestGarbage1();s1=null;
s2=null;System.gc();
}

Conclusion: Thus we studied Exception handling and Garbage collection using JAVA
SARASWATI EDUCATION SOCIETY
YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING AND TECHNOLOGY
DR.N.Y.TASGAONKAR TECHNICAL EDUCATION CAMPUS, CHANDHAI AT.POST:-NASARAPUR, TAL: - KARJAT, DIST: - RAIGAD

EXPERIMENT NO: 8
TITLE: Thread management and synchronization

Aim: Write a program to understand thread management and synchronization

Requirements: jdk 1.8 or higher version,eclipse.

Objective: Understand run time program management

Theory:
Thread is actually a lightweight process. Multithreading is a specialized form of
multitasking and a
multitasking is the feature that allows your computer to run two or more programs
concurrently. In
general, there are two types of multitasking: process-based and thread-based. A
multithreaded
program contains two or more parts that can run concurrently. Each part of such a
program is called a
thread, and each thread defines a separate path of execution. A multithreaded program
contains two
or more parts that can run concurrently. Each part of such a program is called thread
and each thread
defines a separate path of execution. Thus, multithreading is a specialized form of
multitasking.
Threads exist in several states. Following are those states:

New – When we create an instance of Thread class, a thread is in a new state.

Runnable – The Java thread is in running state.

Suspended – A running thread can be suspended, which temporarily suspends its


activity. A
suspended thread can then be resumed, allowing it to pick up where it left off.

Blocked – A java thread can be blocked when waiting for a resource.

Terminated – A thread can be terminated, which halts its execution immediately at


any given
time. Once a thread is terminated, it cannot be resumed.

Methods of thread class


run() Entry point for a thread
sleep() suspend thread for a specified time
start() start a thread by calling run() method
setName() to give thread a name
getPriority() return thread's priority
join() Wait for a thread to end
Conclusion: Thus we studied thread management and synchronization JAVA
SARASWATI EDUCATION SOCIETY
YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING AND TECHNOLOGY
DR.N.Y.TASGAONKAR TECHNICAL EDUCATION CAMPUS, CHANDHAI AT.POST:-NASARAPUR, TAL: - KARJAT, DIST: - RAIGAD

EXPERIMENT NO: 9

TITLE: Installation and working of SWI Prolog


Aim: To install SWI Prolog

Requirements: download Prolog using given link https://www.swi-


prolog.org/download

Objective: To understand installation of SWI Prolog


SARASWATI EDUCATION SOCIETY
YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING AND TECHNOLOGY
DR.N.Y.TASGAONKAR TECHNICAL EDUCATION CAMPUS, CHANDHAI AT.POST:-NASARAPUR, TAL: - KARJAT, DIST: - RAIGAD
SARASWATI EDUCATION SOCIETY
YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING AND TECHNOLOGY
DR.N.Y.TASGAONKAR TECHNICAL EDUCATION CAMPUS, CHANDHAI AT.POST:-NASARAPUR, TAL: - KARJAT, DIST: - RAIGAD

Outcome: understand Installation of Prolog.

Conclusion: Thus we studied Installation and working of SWI Prolog


SARASWATI EDUCATION SOCIETY
YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING AND TECHNOLOGY
DR.N.Y.TASGAONKAR TECHNICAL EDUCATION CAMPUS, CHANDHAI AT.POST:-NASARAPUR, TAL: - KARJAT, DIST: - RAIGAD

EXPERIMENT NO: 10
TITLE: SWI PROLOG

Aim: To understand basics of SWI PROLOG

Requirements: SWI PROLOG

Objective: Design and implement declarative programs in functional and logic programming
language.

Theory: Prolog stands for programming in logic. it is a logic programming language for
artificial intelligence. An artificial intelligence developed in Prolog will examine the link
between a fact, a true statement, and a rule, a conditional statement, in order to come up with
a question, or end objective.
Logic Programming is the name given to a distinctive style of programming.
It is very different from that of conventional programming languages such as C,
C++ and Java.
Prolog is most widely used language.
The name stands for Programming in Logic.
rolog programs are often described as declarative.
There are only two components to any program: facts and rules.
The Prolog system reads in the program and simply stores it.
The user then types in a series of questions, known as queries, which the system
answers using the facts and rules available to it
This is a simple example, a series of queries and answers about animals. The
program consists of just seven lines.
Prolog has been used for a wide variety of applications.
Many of these are in Mathematics and Logic but many are not. Some examples
of the second type of application are:
1. programs for processing a 'natural language' text, to answer questions
about its meaning, translate it to another language etc.
2. advisory systems for legal applications
3. applications for training
4. an electronic support system for doctors

Data Objects in Prolog


Terms have several different types, which are explained as follows:

1. Numbers
In Prolog, all versions allow the use of integers. Any sequence of numbers from 0 to
9 is written as numbers. The numbers are preceded by + or - sign.
The use of numbers with decimal points is allowed by most versions of Prolog. Just
SARASWATI EDUCATION SOCIETY
YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING AND TECHNOLOGY
DR.N.Y.TASGAONKAR TECHNICAL EDUCATION CAMPUS, CHANDHAI AT.POST:-NASARAPUR, TAL: - KARJAT, DIST: - RAIGAD

like the integer, they are written. It contains a single decimal point.
For Example:
278 , -64 , +8 , 057 , 4.32 , -7.91 , +8.45.

2. Atoms
Atoms do not have any numerical value.
Atoms are like the constants.
The atoms can be written in three different ways.
Any sequence of upper case or lower case letters, underscores, numerals,
starting with a lower case letter. For example,
Valid Atoms: jackson , toady_is_Saturday , smart_jack , x64_YZ .
Invalid Atoms: Weekend , Toady-is-Saturday , 64xyz
3. Variable

Variables are like the constants.


The name begins with Uppercase letter or underscore( _ ).
For example: Answer, X, _name, _Z
Prolog Example

dog(fido).
dog(rover).
dog(henry).
cat(felix).
cat(michael).
cat(jane).
animal(X):-dog(X).
The first three lines are facts, with the obvious interpretation that fido, rover and
henry are all dogs. The next three facts say that felix, michael and jane are all
cats. The final line is a rule saying that anything (let us call it X) is an animal if it
is a dog.
Having loaded the program, the user is then faced with the two character symbol
?- which is called the system prompt.
Provide the knowledge base to the system
?- dog(fido).
true.
?- animal(felix).
false.
?- animal(rover).
true.
Outcome: Design and Develop solution based on declarative programming paradigm using
functional and logic programming.

Conclusion: Thus we studied Installation and understand basics of SWI Prolog


SARASWATI EDUCATION SOCIETY
YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING AND TECHNOLOGY
DR.N.Y.TASGAONKAR TECHNICAL EDUCATION CAMPUS, CHANDHAI AT.POST:-NASARAPUR, TAL: - KARJAT, DIST: - RAIGAD

EXPERIMENT NO: 11
TITLE: SWI PROLOG

Aim: To understand basic concepts in SWI PROLOG

Requirements: SWI PROLOG

Objective: Design and implement declarative programs in functional and logic programming
language.
Theory:
Prolog stands for programming in logic. it is a logic programming language for artificial
intelligence. An artificial intelligence developed in Prolog will examine the link between a
fact, a true statement, and a rule, a conditional statement, in order to come up with a question,
or end objective.
Unification in Prolog:
Prolog unification has one or more variables given values to make two call terms as identical,
and this process is called binding variables to values. In prolog unification, prolog agrees that
the two terms to unify, i.e., variables unify with anything, and hence they will unify with each
other. The process in which Prolog matched two terms is known as Prolog unification. The
concept is similar to unification logic. This matching process is done from left to right, and it
fails if there is no match found.
Resolution in Prolog:
Prolog resolution is the process of determining whether a rule can be proved. The process
uses unification to match variables and predicates, but the process known as resolution
concerns the depth-first search process for finding variable assignments that satisfy rules (and
the rules they refer recursively).
Operators in Prolog:
1.Arithmatic Operators
In arithmetic expression, + - * / symbols are special type of infix operator, and
these operators are also known as arithmetic operators.
In Prolog, operators are used as predicates but here operators are functions and
these operators return a numerical value.
Arithmetic expressions can include variables, numbers, operators, and
arithmetic functions.
These will be written in parentheses with their arguments.
These will return numerical values just like the arithmetic operators.
Example 1
?- A is 5.7 + 2.9 * 3
A = 14.4
Example 2
?- B is 6, C is B + 2.
B = 6,
C=8
Example 3
SARASWATI EDUCATION SOCIETY
YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING AND TECHNOLOGY
DR.N.Y.TASGAONKAR TECHNICAL EDUCATION CAMPUS, CHANDHAI AT.POST:-NASARAPUR, TAL: - KARJAT, DIST: - RAIGAD

?- A is sqrt(25).
A=5
Example 4
?- A is 7, B is -A - 3.
A = 7,
B = -10
Example 5
?- A is 30, B is 3, C is A + B + A * B + sin(A).
A = 30,
B = 3,
C = 123.5
Example 6
?- A is 5, A is 4+1.
A = 5.
Example 7
?- A is 5, A is 4+21.
false.
2. Relational Operators
The relational operators are =:=, >, <, >=, =/=, =<.
The relational operators compare the values of two arguments. If the first
argument's value is equal to, greater than, less than, greater than or equal to,
not equal to, less than or equal to the value of second argument, the goal
succeeds.
Both arguments can be arithmetic expression, bound variable or numbers.
Example 1:
?- 60 - 5 + 10 =:= 85 - 10*2.
yes
Example 2:
?- 59<63.
No
Outcome: Design and Develop solution based on declarative programming paradigm using
functional and logic programming.

Conclusion: Thus we studied Operators of SWI Prolog


SARASWATI EDUCATION SOCIETY
YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING AND TECHNOLOGY
DR.N.Y.TASGAONKAR TECHNICAL EDUCATION CAMPUS, CHANDHAI AT.POST:-NASARAPUR, TAL: - KARJAT, DIST: - RAIGAD

EXPERIMENT NO: 12

TITLE: SWI PROLOG

Aim: To study List in SWI PROLOG

Requirements: SWI PROLOG

Objective: Design and implement declarative programs in functional and logic programming
language.

Theory:
A list is a collection of items, not necessarily homogeneous. In Prolog, lists are
inbuilt data structures. Lists can be used to represent sets, stacks, queues, linked
lists, and several complex data structures such as trees, graphs, etc.
A list in Prolog is an ordered collection of items denoted as [i1, i2, …, in].
Unlike arrays in other programming languages where we can directly access any
element of the array, prolog lists allow direct access of the first element only which
is denoted as Head. Therefore we can write a prolog list as : [Head | Rest], where
Rest is the rest of the list excluding the first element Head.
Prolog lists allow nonhomogeneous data types of list items.
Nested lists of arbitrary depths are also allowed in prolog.
Pipe (|) operator:
In prolog, lists have got only one operator, called pipe, denoted by |. This operator
is used to append an element at the beginning of a list. The syntax of the pipe
operator is as follows :
[a | L]
Here L is a list and a is a single element.
For example: If, L = [b,c,d]
Then, [a | L] will result in [a,b,c,d]

Operations in List:

1.To find length of list:

list_length([], 0).
list_length([_ | L], N) :-list_length(L, N1),N is N1 + 1.

?-list_length([a, b, c, d], N).


N=4
?-list_length([1,2,3,4,5],N).
N=5
SARASWATI EDUCATION SOCIETY
YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING AND TECHNOLOGY
DR.N.Y.TASGAONKAR TECHNICAL EDUCATION CAMPUS, CHANDHAI AT.POST:-NASARAPUR, TAL: - KARJAT, DIST: - RAIGAD

2.To find sum of all elements in list:

list_sum([], 0).
list_sum([H | L], N) :-list_sum(L, N1),N is N1 + H.
?- list_sum([1,2,3], N).
N = 6.
?- list_sum([2,3,4], N).
N = 9.

3.To insert elements in list:

insert_end(L, X, NewL) :-append_list(L, [X], NewL).


?-insert_end([1, 2, 3], 7, Ans).
Ans = [1, 2, 3, 7].
?-insert_end([1, 2, 3], 77, Ans).
Ans = [1, 2, 3, 77].

4. Determine whether an element X belongs to a list L

is_member(X, [X | _ ]) :- !.
is_member(X, [ _ | Rest]) :- is_member(X, Rest).
?- is_member(f, [a, b, c, d]).
false.
?- is_member(k, [a, b, c, d]).
false.
?- is_member(d, [a, b, c, d]).
True

Outcome: Design and Develop solution based on declarative programming paradigm using
functional and logic programming.

Conclusion: Thus we studied Operators list in SWI Prolog

You might also like