Unit-6
Logical and Functional Programming
Q1) What is Functional Programming Paradigm in java?
A1) 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. −
Functional Programming – Characteristics
The most prominent characteristics of functional programming are as follows −
Functional Programming – Advantages
Functional programming offers the following advantages −
As a downside, functional programming requires a large memory space. As it does not have state, you need to create new objects every time to perform actions.
Functional Programming is used in situations where we have to perform lots of different operations on the same set of data.
Q2) What is Functional Programming vs. Object-oriented Programming in java?
A2) Functional Programming vs. Object-oriented Programming
The following table highlights the major differences between functional programming and object-oriented programming −
Functional Programming | OOP |
Uses Immutable data. | Uses Mutable data. |
Follows Declarative Programming Model. | Follows Imperative Programming Model. |
Focus is on: “What you are doing” | Focus is on “How you are doing” |
Supports Parallel Programming | Not suitable for Parallel Programming |
Its functions have no-side effects | Its methods can produce serious side effects. |
Flow Control is done using function calls & function calls with recursion | Flow control is done using loops and conditional statements. |
It uses "Recursion" concept to iterate Collection Data. | It uses "Loop" concept to iterate Collection Data. For example: For-each loop in Java |
Execution order of statements is not so important. | Execution order of statements is very important. |
Supports both "Abstraction over Data" and "Abstraction over Behavior". | Supports only "Abstraction over Data". |
Q3) Explain efficiency of a program code
A3) Efficiency of a Program Code
The efficiency of a programming code is directly proportional to the algorithmic efficiency and the execution speed. Good efficiency ensures higher performance.
The factors that affect the efficiency of a program includes −
The efficiency of a programming language can be improved by performing the following tasks −
An efficient programming code can reduce resource consumption and completion time as much as possible with minimum risk to the operating environment.
Q4) Explain in short LISP
A4) LISP - Overview
John McCarthy invented LISP in 1958, shortly after the development of FORTRAN. It was first implemented by Steve Russell on an IBM 704 computer.
It is particularly suitable for Artificial Intelligence programs, as it processes symbolic information effectively.
Common Lisp originated, during the 1980s and 1990s, in an attempt to unify the work of several implementation groups that were successors to Maclisp, like ZetaLisp and NIL (New Implementation of Lisp) etc.
It serves as a common language, which can be easily extended for specific implementation.
Programs written in Common LISP do not depend on machine-specific characteristics, such as word length etc.
Features of Common LISP
Applications Built in LISP
Large successful applications built in Lisp.
Q5) Explain LISP Decision making
A5) LISP - Decision Making
Decision making structures require that the programmer specify one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false.
Following is the general form of a typical decision making structure found in most of the programming languages −
LISP provides following types of decision making constructs. Click the following links to check their detail.
Sr.No. | Construct & Description |
1 | Cond This construct is used for used for checking multiple test-action clauses. It can be compared to the nested if statements in other programming languages. |
2 | if The if construct has various forms. In simplest form it is followed by a test clause, a test action and some other consequent action(s). If the test clause evaluates to true, then the test action is executed otherwise, the consequent clause is evaluated. |
3 | when In simplest form it is followed by a test clause, and a test action. If the test clause evaluates to true, then the test action is executed otherwise, the consequent clause is evaluated. |
4 | Case This construct implements multiple test-action clauses like the cond construct. However, it evaluates a key form and allows multiple action clauses based on the evaluation of that key form. |
Q6) Explain LISP Loops
A6) LISP - Loops
There may be a situation, when you need to execute a block of code numbers of times. A loop statement allows us to execute a statement or group of statements multiple times and following is the general form of a loop statement in most of the programming languages.
LISP provides the following types of constructs to handle looping requirements. Click the following links to check their detail.
Sr.No. | Construct & Description |
1 | Loop The loop construct is the simplest form of iteration provided by LISP. In its simplest form, it allows you to execute some statement(s) repeatedly until it finds a return statement. |
2 | loop for The loop for construct allows you to implement a for-loop like iteration as most common in other languages. |
3 | Do The do construct is also used for performing iteration using LISP. It provides a structured form of iteration. |
4 | dotimes The dotimes construct allows looping for some fixed number of iterations. |
5 | dolist The dolist construct allows iteration through each element of a list. |
Gracefully Exiting From a Block
The block and return-from allows you to exit gracefully from any nested blocks in case of any error.
The block function allows you to create a named block with a body composed of zero or more statements. Syntax is −
(block block-name(
...
...
))
The return-from function takes a block name and an optional (the default is nil) return value.
The following example demonstrates this −
Example
Create a new source code file named main.lisp and type the following code in it −
(defun demo-function(flag)
(print'entering-outer-block)
(block outer-block
(print 'entering-inner-block)
(print(block inner-block
(if flag
(return-from outer-block 3)
(return-from inner-block 5)
)
(print'This-wil--not-be-printed))
)
(print 'left-inner-block)
(print'leaving-outer-block)
t)
)
(demo-function t)
(terpri)
(demo-function nil)
When you click the Execute button, or type Ctrl+E, LISP executes it immediately and the result returned is −
ENTERING-OUTER-BLOCK
ENTERING-INNER-BLOCK
ENTERING-OUTER-BLOCK
ENTERING-INNER-BLOCK
5
LEFT-INNER-BLOCK
LEAVING-OUTER-BLOCK
Q7) Write an Example to define and call a function that would calculate the area of a circle when the radius of the circle is given as an argument.
A7) Let's define and call a function that would calculate the area of a circle when the radius of the circle is given as an argument.
Create a new source code file named main.lisp and type the following code in it.
(defun area-circle(rad)
"Calculates area of a circle with given radius"
(terpri)
(format t "Radius: ~5f" rad)
(format t "~%Area: ~10f"(*3.141592 rad rad))
)
(area-circle 10)
When you execute the code, it returns the following result −
Radius: 10.0
Area: 314.1592
Please note that −
Q8) Explain LISP Predicates in java
A8) LISP - 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.
The following table shows some of the most commonly used predicates −
Sr.No. | Predicate & Description |
1 | Atom It takes one argument and returns t if the argument is an atom or nil if otherwise. |
2 | Equal It takes two arguments and returns t if they are structurally equal or nil otherwise. |
3 | Eq It takes two arguments and returns t if they are same identical objects, sharing the same memory location or nil otherwise. |
4 | 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. |
5 | Evenp It takes one numeric argument and returns t if the argument is even number or nil if otherwise. |
6 | Oddp It takes one numeric argument and returns t if the argument is odd number or nil if otherwise. |
7 | Zerop It takes one numeric argument and returns t if the argument is zero or nil if otherwise. |
8 | Null It takes one argument and returns t if the argument evaluates to nil, otherwise it returns nil. |
9 | Listp It takes one argument and returns t if the argument evaluates to a list otherwise it returns nil. |
10 | 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. |
11 | 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. |
12 | Numberp It takes one argument and returns t if the argument is a number or nil if otherwise. |
13 | Symbol It takes one argument and returns t if the argument is a symbol otherwise it returns nil. |
14 | Integer It takes one argument and returns t if the argument is an integer otherwise it returns nil. |
15 | Rationalp It takes one argument and returns t if the argument is rational number, either a ratio or a number, otherwise it returns nil. |
16 | Floatp It takes one argument and returns t if the argument is a floating point number otherwise it returns nil. |
17 | Realp It takes one argument and returns t if the argument is a real number otherwise it returns nil. |
18 | Complex It takes one argument and returns t if the argument is a complex number otherwise it returns nil. |
19 | Character It takes one argument and returns t if the argument is a character otherwise it returns nil. |
20 | Stringp It takes one argument and returns t if the argument is a string object otherwise it returns nil. |
21 | Arrayp It takes one argument and returns t if the argument is an array object otherwise it returns nil. |
22 | Packagep It takes one argument and returns t if the argument is a package otherwise it returns nil. |
Q9) Write an example to create a new source code file named main.lisp
A9) (write (atom 'abcd))
(terpri)
(write (equal 'a 'b))
(terpri)
(write (evenp 10))
(terpri)
(write (evenp7 ))
(terpri)
(write (oddp7 ))
(terpri)
(write (zerop 0.0000000001))
(terpri)
(write (eq 3 3.0 ))
(terpri)
(write (equal 3 3.0 ))
(terpri)
(write (null nil ))
When you execute the code, it returns the following result −
T
NIL
T
NIL
T
NIL
NIL
NIL
T
Q10) What is Recursion and iteration in java?
A10) The Recursion and Iteration both repeatedly execute the set of instructions. Recursion is when a statement in a function calls itself repeatedly. The iteration is when a loop repeatedly executes until the controlling condition becomes false. The primary difference between recursion and iteration is that recursion is a process, always applied to a function and iteration is applied to the set of instructions which we want to get repeatedly executed.
Recursion
Example
public class RecursionExample {
public static void main(String args[]) {
RecursionExample re = new RecursionExample();
int result = re.factorial(4);
System.out.println("Result:" + result);
}
public int factorial(int n) {
if (n==0) {
return 1;
}
else {
return n*factorial(n-1);
}
}
}
Output
Result:24
Iteration
Example
public class IterationExample {
public static void main(String args[]) {
for(int i = 1; i <= 5; i++) {
System.out.println(i + " ");
}
}
}
Output
1
2
3
4
5