0% found this document useful (0 votes)
26 views10 pages

PPL Unit-5

The document provides an overview of lambda calculus, functional programming languages, and various programming paradigms including Scheme, Prolog, and ML. It explains key concepts such as variables, functions, evaluation, and the differences between functional and object-oriented programming. Additionally, it discusses multi-paradigm languages and their flexibility in problem-solving.

Uploaded by

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

PPL Unit-5

The document provides an overview of lambda calculus, functional programming languages, and various programming paradigms including Scheme, Prolog, and ML. It explains key concepts such as variables, functions, evaluation, and the differences between functional and object-oriented programming. Additionally, it discusses multi-paradigm languages and their flexibility in problem-solving.

Uploaded by

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

Unit-5

1) lambda calculus
Lambda Calculus
Lambda calculus (λ-calculus), is the world's smallest
programming language. Despite not having numbers, strings,
booleans, or any non-function datatype, lambda calculus can be used
to represent any Turing Machine!
Lambda calculus is composed of 3 elements: variables, functions,
and applications.
Name Syntax Example Explanation

Variable <name> x a variable named "x"

a function with parameter


Function λ<parameters>.<body> λx.x
"x" and body "x"

calling the function "λx.x"


Application <function><variable or function> (λx.x)a
with argument "a"

Lambda Calculus’ Syntax


Everything in Lambda Calculus is an expression, which means that
everything must evaluate to a value.
There are, however, four different forms of expressions (which I’ll
call E). An E can be either:
• ID - Identifier
• λID. E - Abstraction.
• E E - Application
• (E) - Grouping
Free vs. Bound Variables:
• In the function λx.x, "x" is called a bound variable because it is
both in the body of the function and a parameter.
• In λx.y, "y" is called a free variable because it is never declared
before hand.

Evaluation:
Evaluation is done via β-Reduction, which is essentially lexically-
scoped substitution.
When evaluating the expression (λx.x)a, we replace all
occurrences of "x" in the function's body with "a".
• (λx.x)a evaluates to: a
Note: Although lambda calculus traditionally supports only single
parameter functions, we can create multi-parameter functions using
a technique called currying.
• (λx.λy.λz.xyz) is equivalent to f(x, y, z) = ((x y) z)

Numbers:

Although there are no numbers in lambda calculus, we can


encode numbers using Church numerals.

0 = λf.λx.x

1 = λf.λx.f x

2 = λf.λx.f(f x)

3 = λf.λx.f(f(f x))
2) Functional programming languages
Functional programming is a declarative programming
paradigm style where one applies pure functions in sequence to
solve complex problems.

• Declarative: FP uses a declarative programming model, where


function definitions are trees of expressions that map values to
other values.
• Immutable data: FP uses immutable data and avoids concepts
like shared states.
• Modularity: FP breaks down large projects into simpler
modules.
Functional Programming has two types; those are shown as below:
o Pure Functional Languages: Pure functional language supports
only the functional pattern. Example- Haskell.
o Impure Functional Language: Impure Functional language
supports the prototype of functions and the programming's
imperative style. Example- LISP.
Characteristics of the Functional Programming Language
• Functional programming languages are designed to perform the
functions of mathematical functions. These functions use
conditional expressions and recursion to perform the
computation.
• Functional programming supports functions in higher-order and
features of lazy evaluation.
• Object-Oriented Programming supports the Abstraction,
Encapsulation, and Polymorphism, just like functional
programming languages support OOPS concepts.
Advantages

o Bugs-Free code:
o Efficient Programming Language:
o Efficiency-
o Supports Nested Functions

Note: Functional programming does not have any state, so all the
time, there is a need to create new objects to perform the actions.
That's why functional programming needs a large memory space.

Functional Programming vs OOPS


Sr.No. Functional Programming Object-Oriented
Programming

The functional programming language


1. OOP uses mutable data.
supports immutable data.

Functional Programming supports the OOP supports the imperative


2.
Declarative Programming Model. Programming Model.

Functional Programming focuses on the OOP focuses on the "How we


3.
"What we are doing". are doing".

The methods of Functional Programming Methods of the OOP can


4.
will not produce any side-effects. produce the side-effects.

Functional Programming follows parallel OOP does not work on parallel


5.
programming. programming.
Object-Oriented Programming
For the iteration of the data collection, uses the "Loop" concept for
6. functional programming uses the the iteration of Data collection.
"Recursion" concept. For example, For-each loop in
Java

It is essential for the oops


For functional programming, the
programming to execute the
8. execution of statements in the order is
statements in order is very
not so important.
important.

Functional Programming supports


OOP supports only
9. "Abstraction over Data" and
"Abstraction over Data".
"Abstraction over Behaviour".

3) Programming with Scheme


Scheme is a classic programming language in the Lisp family. It
emphasizes functional programming and domain-specific
languages but adapts to other styles.
Known for its clean and minimalist design, Scheme is one of the
longest-lived and best-studied dynamic languages.
Data Structure: List
1: Using Scheme to do basic calculations
Any function in scheme must be enclosed by a pair of braces ()
Lets add two number, 1 and 2. 1+2 is written as (+ 1 2) in scheme
Syntax: > (+ 1 2)
The basic types in scheme are integers, floating point numbers,
strings, booleans(#t (true), #f (false)), and symbols.
2.Lists
Scheme does not have C or Java type arrays; lists are the primary
aggregate types. A list can be created using the following command :
> (list 3 4 5 6)
(3 4 5 6)
4) Prolog
Prolog stands for programming in logic. It has important
role in artificial intelligence.
Prolog is a declarative language, which means that a program
consists of data based on the facts and rules
In prolog, logic is expressed as relations (called as Facts and
Rules). Core heart of prolog lies at the logic being applied.
To obtain the solution, the user asks a question rather than
running a program. When a user asks a question, then to determine
the answer, the run time system searches through the database of
facts and rules.
Example:
Format : relation(entity1, entity2, ....k'th entity).

Example :
friends(raju, mahesh).
singer(sonu).
odd_number(5).
Explanation :
These facts can be interpreted as :
raju and mahesh are friends.
sonu is a singer.
5 is an odd number.

Running queries :
Query 1 : ?- singer(sonu).
Output : Yes.
Explanation : As our knowledge base contains
the above fact, so output was 'Yes', otherwise
it would have been 'No'.

Query 2 : ?- odd_number(7).
Output : No.
Explanation : As our knowledge base does not
contain the above fact, so output was 'No'.
Key Features :
1. Unification : The basic idea is, can the given terms be made to
represent the same structure.
2. Backtracking : When a task fails, prolog traces backwards and tries
to satisfy previous task.
3. Recursion : Recursion is the basis for any search in program.
5) ML
➢ A static-scoped functional language with syntax that is closer to
Pascal than to LISP
➢ Uses type declarations, but also does type inferencing to
determine the types of undeclared variables
➢ It is strongly typed (whereas Scheme is essentially typeless) and
has no type coercions
➢ Includes exception handling and a module facility for
implementing abstract data types
➢ Includes lists and list operations
6) Logic Programming:
Logic programming expresses and manipulates logic-based
relationships and rules in a declarative manner.
Logic programs, driven solely by data, utilize logical statements
known as predicates, which are either facts or rules.
Major logic programming language families include Prolog,
Answer Set Programming (ASP) and Datalog.
Functional Programming Logical Programming

It is totally based on functions. It is totally based on formal logic.

Represent facts and rules related


constructed by applying and
to problems within a system of
composing functions.
formal logic.

Absys, Cycl, Alice, ALF (Algebraic


Clojure, Wolfram Language, Erland,
logic functional programming
OCaml, etc.
language), etc.

It reduces code redundancy,


improves modularity, solves complex It is data-driven, array-oriented,
problems, increases maintainability, used to express knowledge, etc.
etc.

It usually supports the functional It usually supports the logic


programming paradigm. programming paradigm.

Testing is comparatively more


Testing is much easier as compared
difficult as compared to functional
to logical programming.
programming.

It simply uses predicates. Here, the


It simply uses functions. predicate is not a function i.e., it
does not have a return value.
7) Multi-paradigm languages
A multi-paradigm programming language is a language that
supports multiple programming styles, or paradigms, such
as: procedural, object-oriented, functional, imperative, and
declarative.
Multi-paradigm languages offer more flexibility and versatility
for solving problems and expressing ideas.
They allow programmers to approach problems from different
perspectives and choose the best way to solve them.
Some examples of multi-paradigm programming languages include:
Python
Supports all the core features of object-oriented programming
(OOP), including abstraction, encapsulation, polymorphism, and
inheritance.
JavaScript
A dynamic language with types and operators, standard built-in
objects, and methods. It supports object-oriented programming with
object prototypes and classes

You might also like