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

Unit-6 Logical and Functional Programming

This document outlines Unit 6 of the Principles of Programming Languages course, focusing on Logical and Functional Programming. It covers key concepts such as the Functional Programming Paradigm, including symbol manipulation, basic LISP functions, recursion, and logic programming with Prolog. The document also discusses the characteristics, advantages, and structure of functional programming languages, particularly LISP.

Uploaded by

karan.ppjgk22
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Unit-6 Logical and Functional Programming

This document outlines Unit 6 of the Principles of Programming Languages course, focusing on Logical and Functional Programming. It covers key concepts such as the Functional Programming Paradigm, including symbol manipulation, basic LISP functions, recursion, and logic programming with Prolog. The document also discusses the characteristics, advantages, and structure of functional programming languages, particularly LISP.

Uploaded by

karan.ppjgk22
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 51

Sinhgad College of Engineering

Vadgaon Bk. Pune


Approved by AICTE New Delhi Recognized by Govt. of Maharashtra
Affiliated to Savitribai Phule Pune University
Accredited by NAAC with A+ Grade

Subject:- 210255: Principles of Programming Languages


Unit-6 Logical & Functional Programming.

Mr. AJIT M. KARANJKAR


Assistant. Professor.
Department of Computer Engineering,
Sinhgad College of Engineering Vadgaon Bk, Pune-041
Email:- [email protected]

16-04-2025 1
Contents:
• Functional Programming Paradigm: Understanding symbol
manipulation, Basic LISP functions, definitions, predicates,
conditionals and scoping, Recursion and iteration, Properties List array
and access functions, Using lambda definitions, printing, reading and
atom manipulation.
• Logic Programming Paradigm: An Overview of Prolog, Syntax and
Meaning of Prolog Programs, Lists, Operators, Arithmetic, Using
Structures.

UNIT-VI Logical & Functional Programming


06-10-2021 2
Department Of Computer Engineering
UNIT-6
LOGICAL AND FUNCTIONAL
PROGRAMMING

UNIT-VI Logical & Functional Programming


06-10-2021 3
Department Of Computer Engineering
6.1 FUNCTIONAL PROGRAMMING
PARADIGM
6.1.1 Understanding symbol manipulation.
6.1.2 Basic LISP functions.
6.1.3 Definitions, predicates, Conditionals and scoping.
6.1.4 Recursion and iteration.
6.1.5 Properties List array and access functions.
6.1.6 Using lambda definitions.
6.1.7 printing, reading and atom manipulation
UNIT-VI Logical & Functional Programming
06-10-2021 4
Department Of Computer Engineering
6.1 Introduction to Functional Programming
• Functional programming languages are specially designed to handle symbolic
computation and list processing applications.

• Functional programming is based on mathematical functions. Some of the popular


functional programming languages include: Lisp, Python, Erlang, Haskell, Clojure, etc.

• Functional programming languages are categorized into two groups, i.e. −

• Pure Functional Languages − These types of functional languages support only the
functional paradigms. For example − Haskell.
• Impure Functional Languages − These types of functional languages support the
functional paradigms and imperative style programming. For example − LISP.

UNIT-VI Logical & Functional Programming


06-10-2021 5
Department Of Computer Engineering
6.1 Introduction to Functional Programming
• Functional Programming – Characteristics

• The most prominent characteristics of functional programming are as follows −

• Functional programming languages are designed on the concept of mathematical


functions that use conditional expressions and recursion to perform computation.

• Functional programming supports higher-order functions and lazy evaluation features.


• Functional programming languages don’t support flow Controls like loop statements and
conditional statements like If-Else and Switch Statements. They directly use the
functions and functional calls.

• Like OOP, functional programming languages support popular concepts such as


Abstraction, Encapsulation, Inheritance, and Polymorphism.
UNIT-VI Logical & Functional Programming
06-10-2021 6
Department Of Computer Engineering
6.1 Introduction to Functional Programming
• Functional Programming – Advantages
• Functional programming offers the following advantages −

• Bugs-Free Code − Functional programming does not support state, so there are no side-
effect results and we can write error-free codes.
• Efficient Parallel Programming − Functional programming languages have NO
Mutable state, so there are no state-change issues. One can program "Functions" to work
parallel as "instructions". Such codes support easy reusability and testability.

• Efficiency − Functional programs consist of independent units that can run concurrently.
As a result, such programs are more efficient.

• Supports Nested Functions − Functional programming supports Nested Functions.


• Lazy Evaluation − Functional programming supports Lazy Functional Constructs like
Lazy Lists, Lazy Maps, etc.
UNIT-VI Logical & Functional Programming
06-10-2021 7
Department Of Computer Engineering
6.1.1 Understanding Symbol Manipulation
Symbol Manipulation Is Like Working with Words and Sentences
• Everything in a computer is a string of binary digits, ones and zeros, that everyone calls
bits.
• From one perspective, sequences of bits can be interpreted as a code for ordinary decimal
digits.
• But from another perspective, sequences of bits can be interpreted as a code for word like
objects and sentence like objects.
• In Lisp, the fundamental things formed from bits are word like objects called atoms.
• Groups of atoms form sentence like objects called lists. Lists themselves can be grouped
together to form higher-level lists.
• Atoms and lists collectively are called symbolic expressions, or more briefly,
expressions.
Working with symbolic expressions is what symbol manipulation using Lisp is about.
UNIT-VI Logical & Functional Programming
06-10-2021 8
Department Of Computer Engineering
6.1.1 Understanding Symbol Manipulation
A symbol-manipulation program uses symbolic expressions to remember and work with
data and procedures, just as people use pencil, paper, and human language to remember and
work with data and procedures.
• A symbol-manipulation program typically has procedures that recognize particular
symbolic expressions, tear existing ones apart, and assemble new ones.

• In LISP, we have the basic data types (s-expression, atom, list):

• S-EXPRESSIONS (or symbolic expressions or simply expressions) = It can be an Atom,


a List, or a collection of S-Expressions enclosed by parentheses.

• NIL is equivalent to the Empty List(i.e. () ), so NIL is both an atom and a list.

UNIT-VI Logical & Functional Programming


06-10-2021 9
Department Of Computer Engineering
6.1.2 Basic Lisp Functions:
• There are three main building blocks of LISP:
• Atoms
• List
• Symbolic Expressions

UNIT-VI Logical & Functional Programming


06-10-2021 10
Department Of Computer Engineering
6.1.2 Basic Lisp Functions:
• Atoms
• ALL expressions in LISP are made up of lists. The list is a linear arrangement of objects
separated by blanks and surrounded by parentheses.

• The objects are made up of either atom. Space-separated elements of a list are known as
ATOMS.

• Example:
name
123
Abc
a
cd UNIT-VI Logical & Functional Programming
06-10-2021 11
Department Of Computer Engineering
6.1.2 Basic Lisp Functions:
• Lists
• Lists are parenthesized collections of sublists or atoms.

• Example:

• (a b (c d) e)
• (10 20 30)

UNIT-VI Logical & Functional Programming


06-10-2021 12
Department Of Computer Engineering
6.1.2 Basic Lisp Functions:
• Symbolic Expressions
• Expressions can be created with the help of operators. There are various arithmetic operators
such as +,-,*, and / used for manipulating the LISP.

• Example:
:LISP - ATOM example
(format t "Adding 2 and 3")
(print(+ 2 3))
(format t "Multiply 2 and 3")
print((* 2 3))
(format t "Subtract 3 from 5")
print((- 5 3))
(format t "Evaluate expression")
print((* (+2 3 )(- 5 3) ))
UNIT-VI Logical & Functional Programming
06-10-2021 13
Department Of Computer Engineering
6.1.2 Basic Lisp Functions:
Lisp Program Structure
• LISP expressions are called symbolic expressions or s-expressions.
• The s-expressions are composed of three valid objects: atoms, lists and strings.
• Any s-expression is a valid program.
• Example - (+ 1 2)
• LISP programs run either on an interpreter or as compiled code
• LISP expressions are case-insensitive, so the below lines are same -
• (write-line "Hello World")
• (WRITE-LINE "Hello World")
• The basic numeric operations in LISP are +, -, *, and /
• LISP represents a function call f(x) as (f x), for example cos(45) is written as (cos 45)

UNIT-VI Logical & Functional Programming


06-10-2021 14
Department Of Computer Engineering
6.1.2 Basic Lisp Functions:
Data Types
• LISP data types can be categorized as.
• Scalar types − for example, number types, characters, symbols etc.
• Data structures − for example, lists, vectors, bit-vectors, and strings.

• The data types are arranged into a hierarchy. A data type is a set of LISP objects and many
objects may belong to one such set.

• The type-predicate is used for finding whether an object belongs to a specific type.
• The type-of function returns the data type of a given object.

UNIT-VI Logical & Functional Programming


06-10-2021 15
Department Of Computer Engineering
6.1.2 Basic Lisp Functions:
Data Types
• LISP data types can be categorized as.
• Scalar types − for example, number types, characters, symbols etc.
• Data structures − for example, lists, vectors, bit-vectors, and strings.

• The data types are arranged into a hierarchy. A data type is a set of LISP objects and many
objects may belong to one such set.

• The type-predicate is used for finding whether an object belongs to a specific type.
• The type-of function returns the data type of a given object.

UNIT-VI Logical & Functional Programming


06-10-2021 16
Department Of Computer Engineering
6.1.2 Basic Lisp Functions:
Prefix / Polish Prefix notation
• No concepts of operators for primitive types
• Simple arithmetic operations also follow function calling syntax
• function_name arg1 arg2... argN
• operator arg1 arg2 ... argN
• (+ 2 5)
•7
• (+ 2 5 3 4 9 20)
• 43
• (+ 2.5 7.25)
• 9.25

• Online LISP interpreter:


https://www.tutorialspoint.com/execute_lisp_online.php
UNIT-VI Logical & Functional Programming
06-10-2021 17
Department Of Computer Engineering
6.1.2 Basic Lisp Functions:
Hello Word
• (write-line "Hello World") ;
• (write-line "I am at ‘SCOE '! Learning LISP")

• Use of Single Quotation Mark


• LISP evaluates everything including the function arguments and list members.
• At times, we need to take atoms or lists literally and don't want them evaluated or treated
as function calls.

• To do this, we need to precede the atom or the list with a single quotation mark.
• (write (* 2 3) > 6
• (write '(* 2 3)) > (* 2 3)

UNIT-VI Logical & Functional Programming


06-10-2021 18
Department Of Computer Engineering
6.1.3 Definitions, Predicates, Conditional & Scoping
• Definitions:
• The Operator defun is used to define the procedure . The syntax of procedure writing in
LISP is

defun(proc_name(argument1,argument2) body of procedure)


For example- procedure for addition of two number is as follows-
>(defun mysum(a,b))
(+a b))
Now we can use this procedure they way we want
>(mysum 2 3)
5

UNIT-VI Logical & Functional Programming


06-10-2021 19
Department Of Computer Engineering
6.1.3 Definitions, Predicates, Conditional & Scoping
Predicates
• Predicates are functions that test their arguments for some specific conditions and returns
nil if the condition is false, or some non-nil value is the condition is true.

• atom
• It takes one argument and returns t if the argument is an atom or nil if otherwise.

• equal
• It takes two arguments and returns t if they are structurally equal or nil otherwise.

• eq
• It takes two arguments and returns t if they are same identical objects, sharing the same
memory location or nil otherwise. UNIT-VI Logical & Functional Programming
06-10-2021 20
Department Of Computer Engineering
6.1.3 Definitions, Predicates, Conditional & Scoping
• eql
• It takes two arguments and returns t if the arguments are eq, or if they are numbers of the
same type with the same value, or if they are character objects that represent the same
character, or nil otherwise.

• evenp
• It takes one numeric argument and returns t if the argument is even number or nil if
otherwise.

UNIT-VI Logical & Functional Programming


06-10-2021 21
Department Of Computer Engineering
6.1.3 Definitions, Predicates, Conditional & Scoping
oddp
• It takes one numeric argument and returns t if the argument is odd number or nil if otherwise.

• zerop
• It takes one numeric argument and returns t if the argument is zero or nil if otherwise.

• null
• It takes one argument and returns t if the argument evaluates to nil, otherwise it returns nil.

• listp
• It takes one argument and returns t if the argument evaluates to a list otherwise it returns nil.

• greaterp
• It takes one or more argument and returns t if either there is a single argument or the arguments are
successively larger from left to right, or nil if otherwise.
UNIT-VI Logical & Functional Programming
06-10-2021 22
Department Of Computer Engineering
6.1.3 Definitions, Predicates, Conditional & Scoping
• lessp
• It takes one or more argument and returns t if either there is a single argument or the
arguments are successively smaller from left to right, or nil if otherwise.

• numberp
• It takes one argument and returns t if the argument is a number or nil if otherwise.

• symbolp
• It takes one argument and returns t if the argument is a symbol otherwise it returns nil.

UNIT-VI Logical & Functional Programming


06-10-2021 23
Department Of Computer Engineering
6.1.3 Definitions, Predicates, Conditional & Scoping
Conditional:
• cond is similar to “if ... then “ –
• Example:
• If x is a list, return its head
(cond
((listp x) (car x)) )
• Example: If x is a list, return its head, otherwise return x itself.
(cond
((lisp x) (car x))
( T x))
Two cond clauses in this example. If
the test in one cond clause is true,
the rest of clause is evaluated and all
other cond clauses are ignored.

T acts like “otherwise”


06-10-2021
UNIT-VI Logical & Functional Programming
Department Of Computer Engineering
24
6.1.3 Definitions, Predicates, Conditional & Scoping
General form:
(cond (test1 exp11 exp12 exp 13 ...)
(test2 exp21 exp22 exp 23 ...)
...
(testn expn1 expn2 expn3 ...) )
• Each of the above list of expressions (1 to n) is called a cond clause.
• Each cond clause can have one to as many expressions. The first expression in each cond
clause is the test.
• The cond clauses will be examined in the order. If the test of a cond clause evaluates to
false, the rest of expressions in that cond clause will be ignored and the next cond clause
will be examined.
• If the test evaluates to true, the rest of the expressions in the cond clause will be evaluated.
The value returned by the last expression is returned as the value of cond. All remaining
cond clauses will be ignored.
• If none of the tests evaluates to true, cond will evaluate to nil.
UNIT-VI Logical & Functional Programming
06-10-2021 25
Department Of Computer Engineering
6.1.3 Definitions, Predicates, Conditional & Scoping
Example:
It accepts 3 arguments. If all arguments are numbers, it returns the minimum. Otherwise it
will return nil.

(defun mymin (x y z)
(cond
((not (and (numberp x)
(numberp y)
(numberp z))) nil)
( (and (<= x y) (<= x z)) x)
( (and (<= y x) (<= y z)) y)
( T z)))
UNIT-VI Logical & Functional Programming
06-10-2021 26
Department Of Computer Engineering
6.1.3 Definitions, Predicates, Conditional & Scoping
Scope
• What is scope?
• Scope refers to the places in a program where a variable is visible and can be referenced.
• Two types of scoping
• Lexical scoping
• Under lexical scoping (also known as static scoping), the scope of a variable is determined by the
lexical (i.e., textual) structure of a program.
• Most programming languages we use today are lexically scoped. Intuitively, a human (or compiler)
can determine the scope of a variable by just examining the source code of a program.

• Dynamic scoping
• Under dynamic scoping, a variable is bound to the most recent value assigned to that variable, i.e.,
the most recent assignment during the program’s execution.
• Dynamically scoped programming languages include bash, LaTeX, and the original version of
Lisp.
• Emacs Lisp is dynamically scoped, but allows the programmer to select lexical scoping.
• Conversely, Perl and Common Lisp are lexically scoped by default, but allow the programmer to
select dynamic scoping. UNIT-VI Logical & Functional Programming
06-10-2021 27
Department Of Computer Engineering
6.1.4 Recursion and Iteration
Recursion:- the repeated application of a recursive procedure or definition.

• Iteration:- the repetition of a process.


• Example:- Finding the Factorial of some number.

• This can easily implemented in 2 ways .

• Using Recursion
• Using Iteration.

UNIT-VI Logical & Functional Programming


06-10-2021 28
Department Of Computer Engineering
6.1.4 Recursion and Iteration
Recursion:- the repeated application of a recursive procedure or definition.

• Iteration:- the repetition of a process.


• Example:- Finding the Factorial of some number.

• This can easily implemented in 2 ways .

• Using Recursion
• Using Iteration.

UNIT-VI Logical & Functional Programming


06-10-2021 29
Department Of Computer Engineering
6.1.4 Recursion and Iteration
Linear Recursion

• Factorial Using the Recursion in


LISP
(define (factorial n)
(if (= n 1)
1
(* n (factorial (- n 1)))))

• Here, Time Complexity is O(x)


• Space Complexity is O(x)

UNIT-VI Logical & Functional Programming


06-10-2021 30
Department Of Computer Engineering
6.1.4 Recursion and Iteration
Iteration

• Factorial Using the Iteration in LISP


(define (factorial n)
(fact-iter 1 1 n))
(define (fact-iter product counter max-count)
(if (> counter max-count)
product
(fact-iter (* counter product)
(+ counter 1)
max-count)))
Here, Time Complexity is O(x)
Space Complexity is O(1)
UNIT-VI Logical & Functional Programming
06-10-2021 31
Department Of Computer Engineering
6.1.5 Properties, A-Lists, Arrays & Access Function
Properties List
• An alternative way to attach data to symbols is to use LISP's property list feature.
• For each symbol, the LISP interpreter maintains a list of properties which can be accessed with the
function get.
• >(get 'mary 'age)
NIL
• >(setf (get 'mary 'age) 45)
45
• >(get 'mary 'age)
45
• Additional properties can be added in the same way.
• If, for some reason, you need to see all the properties a symbol has, you can do so:
>(symbol-plist 'mary)
UNIT-VI Logical & Functional Programming
06-10-2021 32
Department Of Computer Engineering
6.1.5 Properties, A-Lists, Arrays & Access Function
Properties List
• An alternative way to attach data to symbols is to use LISP's property list feature.
• For each symbol, the LISP interpreter maintains a list of properties which can be accessed with the
function get.
• >(get 'mary 'age)
NIL
• >(setf (get 'mary 'age) 45)
45
• >(get 'mary 'age)
45
• Additional properties can be added in the same way.
• If, for some reason, you need to see all the properties a symbol has, you can do so:
>(symbol-plist 'mary)
UNIT-VI Logical & Functional Programming
06-10-2021 33
Department Of Computer Engineering
6.1.5 Properties, A-Lists, Arrays & Access Function
Property List Example
(setf (get 'books 'title) '(Gone with the Wind))
(setf (get 'books 'author) '(Margaret Micheal))
(setf (get 'books 'publisher) '(Warner Books))
(write (get 'books 'title))
(terpri)
(write (get 'books 'author))
(terpri)
(write (get 'books 'publisher))

Output
(GONE WITH THE WIND)
(MARGARET MICHEAL)
(WARNER BOOKS)

UNIT-VI Logical & Functional Programming


06-10-2021 34
Department Of Computer Engineering
6.1.5 Properties, A-Lists, Arrays & Access Function
Property List Example with remove property
(setf (get 'annie 'age) 43)
(setf (get 'annie 'job) 'accountant)
(setf (get 'annie 'sex) 'female)
(setf (get 'annie 'children) 3)
(terpri)
(write (symbol-plist 'annie))
(remprop 'annie 'age)
(terpri)
(write (symbol-plist 'annie))
Output
(CHILDREN 3 SEX FEMALE JOB ACCOUNTANT AGE 43)
(CHILDREN 3 SEX FEMALE JOB ACCOUNTANT)
UNIT-VI Logical & Functional Programming
06-10-2021 35
Department Of Computer Engineering
6.1.5 Properties, A-Lists, Arrays & Access Function
Array and access functions
• LISP allows you to define single or multiple-dimension arrays using the make-array function. An
array can store any LISP object as its elements.

• All arrays consist of contiguous memory locations. The lowest address corresponds to the first
element and the highest address to the last element.

• For example, to create an array with 10- cells, named my-array, we can write −
• (setf my-array (make-array '(10)))
• The aref function allows accessing the contents of the cells. It takes two arguments, the name of the
array and the index value.
• For example, to access the content of the tenth cell, we write −
• (aref my-array 9)

UNIT-VI Logical & Functional Programming


06-10-2021 36
Department Of Computer Engineering
6.1.5 Properties, A-Lists, Arrays & Access Function
Array Example

UNIT-VI Logical & Functional Programming


06-10-2021 37
Department Of Computer Engineering
6.1.6 Using Lamda Functions
Lisp is a functional language based on Alonzo Church’s λ-calculus (which like Turing machines
provides a complete basis for all computable functions).

Syntax:
(lambda (parameter) Body)

• For instance, a function for the square root of the sum of two squares can be expressed as
(lambda (x y) (sqrt (+ (* x x) (* y y)))).

• We can save this function by giving it a name using defun (examples will be seen soon), or we can
apply it directly, as in ((lambda (x y) (sqrt (+ (* x x) (* y y)))) base height) where base and height
are assumed to be variables that have been assigned some numerical values.

UNIT-VI Logical & Functional Programming


06-10-2021 38
Department Of Computer Engineering
6.1.7 Printing, Reading and atom Manipulation
• Printing and Reading: The print write are the output functions which are used to display the
values or text on the console. Whereas read function is for reading the values from the input
streams.

Following program shows the use of printing and reading functions.


Example:
defun myfun()
(print”enter some number:”)
(setq a(read))
(print”you have entered”)
Output:
enter some number: 111
you have entered:111
UNIT-VI Logical & Functional Programming
06-10-2021 39
Department Of Computer Engineering
6.2 FUNCTIONAL PROGRAMMING
PARADIGM
6.2.1 An Overview of Prolog.
6.2.2 Syntax and Meaning of Prolog Programs.
6.2.3 Lists, Operators, Arithmetic.
6.2.4 Using Structures.

UNIT-VI Logical & Functional Programming


06-10-2021 40
Department Of Computer Engineering
6.2.1 Overview of Prolog
One of the most well-known logic programming languages is Prolog
• Stands for PROgramming in LOGic
• Developed by Alain Colmerauer and colleagues in Marseille in the early 1970s

• Prolog is very useful in some problem areas


• such as artificial intelligence, natural language processing, databases, . . .
• But pretty useless in others
• such as for instance graphics or numerical algorithms
• Major logic programming language families include Prolog,
Answer set programming (ASP) and Datalog

Algorithm=Logic+Control
UNIT-VI Logical & Functional Programming
06-10-2021 41
Department Of Computer Engineering
6.2.2 Syntax and Meaning of Prolog Programs
• Variable
• Variable is a string. The string can be a combination of lower case or upper case letters.
The string can also contain underscore characters that begin with an underscore or an
upper-case letter. Rules for forming names and predicate calculus are the same.

• Facts
• A fact is like a predicate expression. It is used to provide a declarative statement about
the problem. In a Prolog expression, when a variable occurs, it is assumed to be
universally quantified. Facts are specified in the form.
Queries
• In Prolog, the query is the action of asking the program about the information which is
available within its database. When a Prolog program is loaded, we will get the query
prompt,
• ?-
UNIT-VI Logical & Functional Programming
06-10-2021 42
Department Of Computer Engineering
6.2.3 List Operators and Arithmetic.
• List is an ordered sequence of objects and lists in prolog, a list written as it is elements
separated by commas and enclosed in brackets . For Example:

• [ ] represent empty list

• [a,b,c,[d]]

• In prolog list elements are enclosed brackets and separated by commas.

• [1,2,3,4]

UNIT-VI Logical & Functional Programming


06-10-2021 43
Department Of Computer Engineering
6.2.3 List Operators and Arithmetic.
Operators
• notation contains a number of arguments in parenthesis like likes(hary, jack).
• Any user-defined predicate which has two arguments can be converted into an infix
operator as an alternative. In this, we can write the functor between the two arguments,
and they have no parenthesis like

• hary likes jack


• Any user-defined predicate which has one argument can be converted into a prefix
operator. In this, we can write the functor before the arguments, and they have no
parenthesis like
• isa_cat bombay
• Instead of isa_cat(bombay)
• Alternatively, we can convert a unary operator into a postfix operator. In this, we can
write the functor after the argument.
UNIT-VI Logical & Functional Programming
06-10-2021 44
Department Of Computer Engineering
6.2.3 List Operators and Arithmetic.
• bombay is a_cat
• The rules can also use the operator notation to aid readability. Some users of Prolog may
find a rule like

• likes(hary,A) :- is_female(A),owns(A,B), isa_dog(B).


• If the above rule is written as follow, it is easier to understand:

• hary likes A :- A is_female, A owns B, B isa_cat.


• If preferred, the operator can use the standard notation 'functor and argument'. 'Mixed'
notation is also permitted if likes/2, is_female/1, owns/2, and isa_dog/1 are all operators
and a valid form of the previous rule.

• likes(hary,X) :- is_female(A), A owns B ,isa_cat(B)


UNIT-VI Logical & Functional Programming
06-10-2021 45
Department Of Computer Engineering
6.2.3 List Operators and Arithmetic.
Various built-in predicates have been pre-defined as an operator. To compare the numerical
value, these include relational operator like < that denote 'greater than' and > that denotes
'less than'.

In the body of the rule, the following terms may be included.

A>3
B<C
X=Y
The built-in predicate is also used the bracket notation that is defined as an operator.

For example
>(A, 3) instead of A>3. .
UNIT-VI Logical & Functional Programming
06-10-2021 46
Department Of Computer Engineering
6.2.3 List Operators and Arithmetic.
Arithmetic:
The operator = is used for unitification in prolog . Following example illustrates the use of
=for binding with the corresponding value.
Example:
1 ?. A=10+20
A=10+20

2 ?. A=10+20
A=30

3 ?. A=10+20
false.
4 ?. | UNIT-VI Logical & Functional Programming
06-10-2021 47
Department Of Computer Engineering
6.2.4 Using Structure
• 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 is an ordered sequence of objects and lists. In prolog a list is written as its
elements separated by comma and enclosed in brackets

• Example
[ ] represents empty list
[a,b,c,[d]]

UNIT-VI Logical & Functional Programming


06-10-2021 48
Department Of Computer Engineering
6.2.4 Using Structure
• The list contains Head and Tail Part. For example – if we type following query
• ?-[H | T]=[10,20,30,40]
We will get
H=10,
T=[20,30,40]

Now consider following execution of the list.


3?.[a,b,c]=[X,Y,Z | T]
X=a,
Y=b,
Z=c
T=[ ]
4 ?.|

Now if we execute
?-[a,b,c]=[a|[b|[c|[ ]]]]
Then we will get-
true 06-10-2021 UNIT-VI Logical & Functional Programming
49
Department Of Computer Engineering
6.2.5 Functional vs Logical Programming
• Functional
• From this illustration, we can see that in Functional Programming, we have to define the
procedures, and the rule how the procedures work.
• These procedures work step by step to solve one specific problem based on the
algorithm.

• Logical Programming
• On the other hand, for the Logic Programming, we will provide knowledge base.

• Using this knowledge base, the machine can find answers to the given questions, which
is totally different from functional programming.

UNIT-VI Logical & Functional Programming


06-10-2021 50
Department Of Computer Engineering
6.2.5 Functional vs Logical Programming
• Example:

UNIT-VI Logical & Functional Programming


06-10-2021 51
Department Of Computer Engineering

You might also like