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

1. Programming Paradigms

The document outlines various programming paradigms including imperative, procedural, object-oriented, parallel processing, declarative, logic, and functional programming, along with their characteristics and examples. It also discusses language characteristics, comparing compiled and interpreted languages, as well as static and dynamic type checking. Additionally, the document covers abstract data types (ADTs), their features, advantages, and disadvantages, emphasizing encapsulation, data abstraction, and information hiding.

Uploaded by

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

1. Programming Paradigms

The document outlines various programming paradigms including imperative, procedural, object-oriented, parallel processing, declarative, logic, and functional programming, along with their characteristics and examples. It also discusses language characteristics, comparing compiled and interpreted languages, as well as static and dynamic type checking. Additionally, the document covers abstract data types (ADTs), their features, advantages, and disadvantages, emphasizing encapsulation, data abstraction, and information hiding.

Uploaded by

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

23CSE212– PRINCIPLES OF FUNCTIONAL

LANGUAGES

Programming Paradigms
&
Language Characteristics
PROGRAMMING PARADIGM
• Paradigm: A pattern or model
• Programming Paradigm: A Pattern or model of
Programming
• A fundamental Style of Computer
Programming
TYPES OF PROGRAMMING PARADIGMS
IMPERATIVE PARADIGM
• One of the oldest programming paradigm
• Works by changing the program state step by step through
assignment statements
• Consists of a set of commands for the computer to perform
• Describes how the inputs are given and outputs are obtained
• Example: C,C++, Java, Python, Ruby etc.

Advantage Disadvantage
Simple to implement Complex problems are difficult to
implement
Contains loops, variables etc. Less efficient and less productive
PROCEDURAL PROGRAMMING PARADIGM

• Emphasizes on procedure in terms of under lying


machine model
• No difference in between procedural and
imperative approach
• Has the ability to reuse the code
• Example: C,C++, Java
OBJECT ORIENTED PROGRAMMING
• Program is written as a collection of classes and object
which are meant for communication
• The smallest and basic entity is object and all kind of
computation is performed on the objects only.
• More emphasis is on data rather procedure.
• Can handle almost all kind of real life problems
• Advantages: Data Security, Inheritance, Code
Reusability, Abstraction and Flexibility
• Example: C++, Java, Python, Ruby etc.
PARALLEL PROCESSING APPROACH
• Processing of program instructions by dividing them
among multiple processors
• Lesser Execution Time
• Divide and Conquer based approach
• Example: Java
DECLARATIVE PARADIGM
• Style of building programs that expresses logic of
computation without talking about its control flow
• Often considers programs as theories of some logic
• Focus is on what needs to be done rather how it should be
done
• Imperative (how to do) and declarative (what to do)
• Categorised as Logic, Functional and Database
LOGIC PROGRAMMING PARADIGM
• An abstract model of computation
• To solve logical problems like puzzles, series etc.
• Main emphasize is on knowledge base and the problem
• Program execution is very much like the proof of a mathematical
statement
• Example: Prolog
• sum of two number in prolog:
predicates
sumoftwonumber(integer, integer)
clauses
sum(0, 0).
sum(n, r):-
n1=n-1,
sum(n1, r1),
r=r1+n
FUNCTIONAL PROGRAMMING PARADIGM
• Has its roots in mathematics
• Language independent
• Key principle - execution of series of mathematical
functions
• Functions are the central model of abstraction
• Functions are meant for specific computation
• Data are loosely coupled to functions
• Functions hide their implementation
• Functions can be replaced with their values without
changing the meaning of the program
Example: Perl, Javascript, Haskell, Scala, Erlang, Lisp,
ML, Clojure etc.
DATABASE
• Programming methodology is based on data and its movement
• Program statements are defined by data rather than hard-coding a series
of steps.
• DB is the heart of a business information system
• Provides file creation, data entry, update, query and reporting functions
• Several programming languages are developed mostly for database
application
• Example: SQL
• Applied to streams of structured data, for filtering, transforming,
aggregating (such as computing statistics), or calling other programs.
CREATE DATABASE databaseAddress; CREATE TABLE Addr
( PersonID int, LastName varchar(200), FirstName
varchar(200), Address varchar(200), City
varchar(200), State varchar(200) );
LANGUAGE CHARACTERISTICS – COMPILED VS
INTERPRETED LANGUAGES
Compiled Languages Interpreted Languages
Two steps are followed to execute the code The code is executed in a single step

A code written in a compiled language has to be The program is executed directly with any
converted to a machine code for execution compilation

Faster execution speed and better performance The speed of execution is slow but more portable
code

CPU executes the code As the code is interpreted, it cannot be executed by


the CPU

Errors prevent the compiling of codes All debugging has to be done at run time

Examples of compiled languages are C, C++, C#, Examples of interpreted languages are JavaScript,
COBOL, etc. Perl, BASIC, Python, etc.

The program is processed as a whole Each instruction is interpreted once at a time

The compiler is used only once if no changes are The interpreter runs each time to execute the code
made to the code

RAM is not needed to execute the code RAM is needed to execute the code
TYPE SYSTEMS – STATIC AND DYNAMIC TYPE
CHECKING
• Typing:
o When is data type is checked?
 Static – Types are checked before run-time
 Dynamic – Types are checked on the fly, during execution
o For example "3" + 5 will raise a type error in strongly typed languages
(E.g., Python and Go)
o Above languages do not allow for "type coercion" - ability to change
types of values based on context (e.g., merging two types using +).
EXAMPLE – PYTHON (DYNAMIC, INTERPRETED)

def foo(a):
if(a>0):
print ('Hi')
else:
print ("3"+5)
foo(2)

• Translates and type checks the code which is executed.


• Else block never executes
• "3" + 5 is ignored.
WHAT IF STATICALLY TYPED?!
• Type error would be thrown even before the code is run.
• Even though interpreted, it performs type checking before run-time.

WHAT IF COMPILED?
• Type error would be thrown even before the code is run, at compilation
time.
EXAMPLE – GO (STATIC, COMPILED)
package main
import "fmt"
func foo(a int) { /tmp/rC7FHazS50/main.go:8:21: invalid
if(a>0){ operation: "3" + 5 (mismatched types
fmt.Println("Hi") untyped string and untyped int) === Code
} else { Exited With Errors ===
fmt.Println("3"+5)
}
}
func main() {
foo(2)
}
• Types are checked before running (static) and type errors are caught
immediately
• If it was interpreted, type errors would still be checked before run-time
• If dynamic, no errors thrown, although the code is checked during compilation
PERFORMANCE
• A compiled language will have better performance at run-time if it's
statically typed as the knowledge of types allows for machine code
optimization
• Statically typed languages have better performance at runtime as they
need not check types dynamically at runtime
• Similarly compiled languages are faster at runtime as the code is already
translated instead of interpretation/translation at runtime
• Both compiled and translated languages will have a delay before running
for translation and type-checking respectively.
TYPECHECKING - SUMMARY
• Static typing catches errors early (useful for long programs) instead of at
execution time
• More strict
• Doesn’t allow type errors anywhere in your program
• Prevents variables from changing types
• Defends against unintended errors
• e.g., num=2
• num='3' //error
• Dynamic typing is more flexible but allows variables to change types,
sometimes creating unexpected errors.
INFORMATION HIDING
• Hiding internal object details, i.e., data members
• Restrict the data access to class members
• Maintains data integrity
Data Hiding vs Encapsulation
• S.No Data Hiding Encapsulation
1 Hides class data components only Hides the private methods and class data parts
2 Restricts the data use to assure data Wraps up the complex data to present a simpler
security view to the user

Access Specifiers:
• Private members/methods: Functions and variables declared as private can be accessed only
within the same class, and they cannot be accessed outside the class they are declared.
• Public members/methods: Functions and variables declared under public can be accessed from
anywhere.
• Protected members/methods: Functions and variables declared as protected cannot be accessed
outside the class except a child class. This specifier is generally used in inheritance.
GROUPING DATA AND OPERATIONS - ENCAPSULATION

• Technically known as "Encapsulation". // Encapsulation using private modifier


• Mechanism that binds together the code and the
data it manipulates // Employee class contains private data
// called employee id and employee name
• Prevents the data from being accessed by the class Employee {
outside code private int empid;
• The variables or the data in a class are hidden private String ename;
from any other class
• These variables can be accessed only through // Accessing the Element
any member function of the class in which they // Using Method
are declared. public int get_id(){
return empid;
• Encapsulation is similar to what data- }
hiding does.
• The terms “encapsulation” and “data-hiding” are public String get_name(){
used interchangeably. return get_name;
• Encapsulation can be achieved by declaring all }
the variables in a class as private and writing }
public methods in the class to set and get the
values of the variables.
ABSTRACT DATA TYPES (ADT S)
• Inbuilt data types:
o int, float, double, long, etc.
o Basic operations possible - addition, subtraction, division, multiplication, etc.
• ADTs:
o Non-inbuilt data structures for user-defined purposes
o Defines data structures along with their operations
o It is a type (or class) for objects whose behavior is defined by a set of values and a set of
operations
o Definition only mentions what operations are to be performed but not how these operations will
be implemented
o Does not specify how data will be organized in memory and what algorithms will be used for
implementing the operations
o Called “abstract” because it gives an implementation-independent view
ABSTRACT DATA TYPES (ADT S)

• The user of data type needs to know what a data type can do and does not need to know
how that data type is implemented
• E.g., Primitive types such as int, float, char data types are used only with the knowledge
of the operation that can be performed on these types, without any idea of how they are
implemented
• Think of ADT as a black box which hides the inner structure and design of the data type.
EXAMPLE 1 – LIST ADT
• The data is generally stored in key sequence in a list which has a head structure
consisting of count, pointers and address of compare function needed to compare the
data in the list.
• The data node contains the pointer to a data structure and a self-referential pointer which
points to the next node in the list.

• List ADT Functions:


• get() – Return an element from the list at any given position.
• insert() – Insert an element at any position of the list.
• remove() – Remove the first occurrence of any element from a non-empty list.
• removeAt() – Remove the element at a specified location from a non-empty list.
• replace() – Replace an element at any position by another element.
• size() – Return the number of elements in the list.
• isEmpty() – Return true if the list is empty, otherwise return false.
• isFull() – Return true if the list is full, otherwise return false.
EXAMPLE 2 – STACK ADT
• In Stack ADT Implementation instead of data being stored in each node, the pointer to
data is stored.
• The program allocates memory for the data and address is passed to the stack ADT.
• The head node and the data nodes are encapsulated in the ADT. The calling function can
only see the pointer to the stack.
• The stack head structure also contains a pointer to top and count of number of entries
currently in stack.
• push() – Insert an element at one end of the stack called top.
• pop() – Remove and return the element at the top of the stack, if it is not empty.
• peek() – Return the element at the top of the stack without removing it,
if the stack is not empty.
• size() – Return the number of elements in the stack.
• isEmpty() – Return true if the stack is empty, otherwise return false.
• isFull() – Return true if the stack is full, otherwise return false.
EXAMPLE 3 – QUEUE ADT
• The queue abstract data type (ADT) follows the basic design of the stack abstract data
type.
• Each node contains a void pointer to the data and the link pointer to the next element in
the queue. The program’s responsibility is to allocate memory for storing the data.
• enqueue() – Insert an element at the end of the queue.
• dequeue() – Remove and return the first element of the queue, if the queue is not empty.
• peek() – Return the element of the queue without removing it, if the queue is not empty.
• size() – Return the number of elements in the queue.
• isEmpty() – Return true if the queue is empty, otherwise return false.
• isFull() – Return true if the queue is full, otherwise return false.
KEY FEATURES OF ADT:
• Better Conceptualization: Gives a better conceptualization of the real world
• Robust: Improved ability to trap errors thereby increasing robustness

• Encapsulation: Hide the internal details of the data and provide a public interface for
users to interact with the data. Hence allows easier maintenance and modification of
the data structure
• Data Abstraction: Provides a level of abstraction from the implementation details of
the data. Users only need to know the operations that can be performed on the data,
not how those operations are implemented.
• Data Structure Independence: ADTs can be implemented using different data
structures, such as arrays or linked lists, without affecting the functionality.
• Information Hiding: ADTs can protect the integrity of the data by allowing access only
to authorized users and operations thereby preventing errors and misuse of data.
• Modularity: ADTs can be combined with other ADTs to form larger, more complex data
structures, which allows for greater flexibility and modularity in programming
ADVANTAGES AND DISADVANTAGES OF ADT
S.No Advantages Disadvantages
1 Encapsulation: Data and operations Overhead: Implementing ADTs can add
encapsulated into a single unit, making it easier overhead in terms of memory and processing,
to manage and modify the data structure. which can affect performance.

2 Abstraction: Allow users to work with data Complexity: ADTs can be complex to
structures without having to know the implement, especially for large and complex data
implementation details, which can simplify structures
programming and reduce errors.

3 Data Structure Independence: Can be Learning Curve: Using ADTs requires


implemented using different data structures, knowledge of their implementation and usage,
making it easier to adapt to changing needs and which can take time and effort to learn.
requirements.

4 Information Hiding: Can protect the integrity of Limited Flexibility: Some ADTs may be limited
data by controlling access and preventing in their functionality or may not be suitable for all
unauthorized modifications. types of data structures.

5 Modularity: Can be combined with other ADTs Cost: Implementing ADTs may require additional
to form more complex data structures, which resources and investment, which can increase
can increase flexibility and modularity in the cost of development.
programming.

You might also like