0% found this document useful (0 votes)
18 views9 pages

Unit 5

Uploaded by

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

Unit 5

Uploaded by

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

Functional programming is a programming paradigm that treats

computation as the evaluation of mathematical functions and avoids


changing-state and mutable data. Here are some fundamental concepts in
functional programming:

1. Pure Functions:
 A pure function is a function where the output is solely determined
by its input parameters, and it has no side effects.
 It always produces the same output for the same input and does not
modify any external state.
2. Immutability:
 In functional programming, data is typically immutable. Once a piece
of data is created, it cannot be changed. Instead of modifying existing
data, new data is created.
3. First-Class and Higher-Order Functions:
 Functions are treated as first-class citizens, meaning they can be
passed as arguments to other functions, returned as values from
other functions, and assigned to variables.
 Higher-order functions are functions that take other functions as
arguments or return them as results.
4. Recursion:
 Recursion is a common technique in functional programming where a
function calls itself to solve smaller instances of a problem.
 Loops are often replaced with recursion in functional languages.
5. Referential Transparency:
 An expression is called referentially transparent if it can be replaced
with its value without changing the program's behavior.
 Pure functions and immutability contribute to referential
transparency.
6. Lambda Calculus:
 Lambda calculus is a mathematical formalism that underlies
functional programming. It uses anonymous functions (lambda
expressions) and function application as its basic building blocks.
7. Map, Filter, and Reduce:
 These are higher-order functions commonly used in functional
programming.
 map applies a function to each element of a list.
 filter returns a new list containing only the elements that satisfy a
given predicate.
 reduce (or fold) combines the elements of a list to produce a single
value.
8. Pattern Matching:
 Pattern matching is a powerful and expressive way to destructure
data in functional languages. It allows you to match data structures
and extract components in a concise manner.
9. Lazy Evaluation:
 Lazy evaluation is a strategy where the evaluation of an expression is
delayed until its value is actually needed.
 This can improve efficiency by avoiding unnecessary computations.
10.Monads:
 Monads are a design pattern used to handle side effects in a
functional way. They provide a structure for chaining operations that
may have side effects.

Functional programming languages, such as Haskell, Scala, and Clojure,


embody these concepts, but you can also apply functional programming
principles in languages like JavaScript, Python, and others.

Scheme is a programming language that follows the functional programming


paradigm. It is a dialect of Lisp and is known for its simplicity and minimalistic syntax.
Here are some key concepts and features of Scheme in the context of functional
programming:

1. Functional Programming Paradigm:


 Scheme is a functional programming language, which means that functions
are first-class citizens. Functions can be assigned to variables, passed as
arguments to other functions, and returned as values from functions.
2. Immutable Data:
 In functional programming, data is typically immutable, meaning that once a
value is assigned to a variable, it cannot be changed. Scheme encourages the
use of immutable data structures.
3. First-Class Functions:
 Functions in Scheme are first-class citizens, meaning they can be treated like
any other data type. You can pass functions as arguments to other functions,
return them as values, and assign them to variables.
4. Higher-Order Functions:
 Scheme supports higher-order functions, which are functions that take other
functions as arguments or return functions as results. This enables powerful
and expressive programming constructs.
5. Lexical Scope:
 Scheme uses lexical scope, where the scope of a variable is determined by its
location in the source code. This is in contrast to dynamic scoping used in
some other Lisp dialects.
6. Recursion:
 Recursion is a fundamental concept in functional programming, and Scheme
encourages the use of recursion for solving problems. Scheme does not have
traditional loop constructs like those found in imperative languages.
7. Lambda Expressions:
 Lambda expressions are used to define anonymous functions in Scheme. They
are particularly useful when passing functions as arguments to higher-order
functions.
8. Tail Recursion:
 Scheme optimizes tail-recursive functions, making them more memory-
efficient. Tail recursion is a special type of recursion where the recursive call is
the last operation in the function.
9. Cons Cells and Lists:
 Scheme uses cons cells to build lists. Lists are a fundamental data structure in
Scheme, and they can be easily manipulated using built-in functions.

Here's a simple example of a recursive function in Scheme that calculates the


factorial of a number:

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

seems like there might be a small typo in your question, as "functional rogramming"
should probably be "functional programming." Assuming that's the case, I'll provide
information about functional programming in the context of ML (Meta Language) or
OCaml, which is a functional programming language derived from ML.

Functional programming is a programming paradigm that treats computation as the


evaluation of mathematical functions and avoids changing state and mutable data.
ML is a family of functional programming languages that includes Standard ML (SML)
and OCaml.

Here are some key features and concepts of functional programming in ML:
1. First-class functions: Functions are first-class citizens, which means they can be
passed as arguments to other functions, returned as values, and assigned to
variables.
2. Immutability: Data in functional programming languages is typically immutable.
Once a value is assigned to a variable, it cannot be changed. Instead of modifying
existing data structures, functional programming encourages creating new ones.
3. Higher-order functions: Functions can take other functions as arguments and/or
return functions as results. This allows for the creation of more abstract and reusable
code.
4. Pattern matching: Pattern matching is a powerful feature in ML languages. It allows
you to destructure data structures and match them against specific patterns, making
it easier to work with complex data types.
5. Recursion: Functional programming languages often rely heavily on recursion for
iteration, instead of traditional looping constructs. Recursive functions can express
computations in an elegant and concise manner.
6. Type inference: ML languages use static typing, but they also employ type
inference. This means you don't always have to explicitly declare types; the compiler
can often infer them based on the context.

Here's a simple example in OCaml demonstrating some of these concepts:

Prolog (Programming in Logic) is a declarative programming language


often used for symbolic reasoning and manipulation. It's based on the
principles of formal logic and is particularly well-suited for tasks involving
rule-based systems, artificial intelligence, and natural language processing.
Here's a brief overview of programming in Prolog:

Basics of Prolog:
1. Facts and Rules:
 Prolog programs are made up of facts and rules.
 Facts are statements about the world. For example:
prologCopy code
likes(john, pizza)
 Rules are used to derive new facts. For example:
prologCopy code
likes(john, X) :- pizza(X), vegetarian(X).
2. Variables:
 Variables in Prolog start with an uppercase letter or an underscore.
prologCopy code
likes(john, X) :- pizza(X), vegetarian(X).
3. Queries:
 Prolog programs are queried to find solutions.
prologCopy code
?- likes(john, What).
4. Unification:
 Unification is a key concept in Prolog. It's the process of making two
terms identical.
prologCopy code
father(john, jim).
father(john, sara).
 Query: father(john, X).

Basic Example:
Let's create a simple Prolog program representing family relationships:

prologCopy code
% Facts
parent(john, jim).
parent(john, sara).
parent(sara, ann).
parent(jim, bob).

male(john).
male(jim).
male(bob).

female(sara).
female(ann).

% Rules
father(X, Y) :- parent(X, Y), male(X).
mother(X, Y) :- parent(X, Y), female(X).

With this program, you can query relationships. For example:

prologCopy code
?- father(john, jim). % true
?- mother(sara, ann). % true
?- parent(john, ann). % false
?- male(sara). % false
?- male(john). % true
Prolog Execution:
1. Backtracking:
 Prolog uses a depth-first search with backtracking to find solutions.
 It will try to satisfy the goal and backtrack if necessary.
2. Cut Operator (!):
 The cut operator ! is used to prevent backtracking. Once a clause
with a cut is satisfied, Prolog won't try alternatives.
prologCopy code
% Example with cut
likes(john, pizza) :- !.
likes(john, salad).

Advanced Concepts:
1. Recursion:
 Prolog supports recursion. It's often used for tasks like traversing
trees or lists.
2. Lists:
 Lists are a fundamental data structure in Prolog.
prologCopy code
% Define a list
my_list([1, 2, 3, 4]).
3. Predicate Order:
 The order of predicates matters in Prolog. The program may give
different results if the order is changed.
4. Cut Operator:
 The cut operator ( !) is used to commit to a choice, preventing
backtracking.

Example with Lists and Recursion:


prologCopy code
% Sum of elements in a list
sum_list([], 0).
sum_list([Head|Tail], Sum) :- sum_list(Tail, TailSum), Sum is Head + TailSum.

Query:

?- sum_list([1, 2, 3, 4], Result).


% Result will be 10
?- sum_list([1, 2, 3, 4], Result). % Result will be 10

This is just a brief introduction to Prolog programming. There's a lot more


to explore, including advanced topics like constraints, meta-programming,
and integration with other languages. Prolog is a powerful tool for solving
problems that can be expressed as logical relationships.

Logic is a branch of philosophy and mathematics that studies the


principles of reasoning, inference, and the structure of valid arguments. It
provides a systematic way to evaluate and analyze the relationships
between statements and propositions. Logical reasoning is fundamental to
various disciplines, including mathematics, computer science, philosophy,
linguistics, and artificial intelligence.

Logical programming, on the other hand, is a subfield of computer science


that involves the use of formal logic to express and execute programs.
Unlike traditional imperative programming languages that focus on
describing how a computation should be performed, logical programming
languages emphasize the declarative specification of what the desired
outcome is.

One of the most well-known logical programming languages is Prolog


(Programming in Logic). In Prolog, programs are expressed as a set of
logical rules and facts. The language is particularly suited for tasks that
involve searching through large datasets for solutions to a given problem.
Prolog uses a form of logic called Horn clause logic, which is based on a set
of rules known as Horn clauses.

Key concepts in logic and logical programming include:

1. Propositional Logic: Deals with propositions, which are statements that


are either true or false. Logical operators such as AND, OR, and NOT are
used to combine propositions.
2. Predicate Logic: Extends propositional logic by introducing predicates and
quantifiers. Predicates are functions that return true or false, and quantifiers
(like "forall" and "exists") express statements about all or some members of
a set.
3. Inference Rules: These are rules that allow us to derive new true
statements from existing true statements. Common inference rules include
modus ponens, modus tollens, and the law of contrapositive.
4. Resolution: A technique used in logical programming to derive new logical
consequences by resolving conflicting propositions.
5. Prolog: A popular logical programming language that uses a form of logic
programming based on first-order predicate logic. Prolog programs consist
of a set of rules and facts, and queries can be posed to find solutions.

Logical programming languages like Prolog are often used in areas such as
expert systems, natural language processing, and database querying. They
provide a different paradigm for expressing and solving problems, focusing
on the logical relationships and constraints within a problem domain.

In summary, logic forms the theoretical foundation for logical


programming, and these concepts are applied in computer science to
design programming languages that leverage formal logic for problem-
solving.
Multi-paradigm programming languages are languages that
support multiple programming paradigms, which are approaches or styles
of programming. Each paradigm represents a different way of thinking
about and structuring code. The main programming paradigms include:

1. Imperative Programming:
 Focuses on describing how a program operates by specifying a series
of statements that change a program's state.
2. Declarative Programming:
 Focuses on describing what a program should accomplish without
specifying the step-by-step procedures for achieving the result.
3. Object-Oriented Programming (OOP):
 Organizes code into objects that encapsulate data and behavior. It
emphasizes the concepts of classes, objects, inheritance, and
polymorphism.
4. Functional Programming:
 Treats computation as the evaluation of mathematical functions and
avoids changing-state and mutable data. Functions are first-class
citizens, allowing for higher-order functions and immutability.
5. Procedural Programming:
 Organizes code into procedures or routines that perform specific
tasks. It follows a linear flow of control.
6. Logic Programming:
 Describes the relationships and constraints between different
elements in a program using logic-based rules.
7. Event-Driven Programming:
 Focuses on responding to events or user actions, often used in
graphical user interfaces and interactive applications.

Some popular multi-paradigm languages include:

1. Python:
 Supports imperative, object-oriented, and procedural programming.
It also includes features from functional programming.
2. C++:
 Combines imperative, object-oriented, and generic programming. It is
an extension of the C programming language.
3. Java:
 Supports object-oriented programming and has some functional
programming features. It is designed to be platform-independent.
4. C#:
 Primarily an object-oriented language with support for imperative
and declarative programming.
5. JavaScript:
 Initially designed for scripting in web browsers, it supports both
object-oriented and functional programming paradigms.
6. Scala:
 Runs on the Java Virtual Machine (JVM) and is a fusion of object-
oriented and functional programming.

These languages offer flexibility and allow developers to choose the most
suitable paradigm for different aspects of their projects. The choice of a
paradigm depends on the problem domain, developer preferences, and
project requirements.

You might also like