Unit-6 Logical and Functional Programming
Unit-6 Logical and Functional Programming
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.
• 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.
• 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.
• NIL is equivalent to the Empty List(i.e. () ), so NIL is both an atom and a list.
• 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)
• 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)
• 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.
• 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.
• 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)
• 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.
• 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.
(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.
• Using Recursion
• Using Iteration.
• Using Recursion
• Using Iteration.
Output
(GONE WITH THE WIND)
(MARGARET MICHEAL)
(WARNER BOOKS)
• 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)
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.
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:
• [a,b,c,[d]]
• [1,2,3,4]
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]]
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.