Unit 5
Unit 5
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.
(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.
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.
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).
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.
Query:
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.
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.
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.