PPL Unit-5 Material
PPL Unit-5 Material
Mathematical Functions:
Def: A mathematical function is a mapping of members of one set, called the domain set, toanother
set, called the range set.
A lambda expression specifies the parameter(s) and the mapping of a function in the following form
f(x) x * x * x for the function cube (x) = x * x *x functions.
– Lambda expressions are applied to parameter(s) by placing the parameter(s) after the expression
1. Function Composition:
A functional form that takes two functions as parameters and yields a function whose result is a
function whose value is the first actual parameter function applied to the result of the application of
the second Form: h(f )° g which means h (x) f ( g ( x))
2. Construction:
A functional form that takes a list of functions as parameters and yields a list of the results of
applying each of its parameter functions to a given parameter.
Form: [f, g]
For f (x) = x * x * x and g (x) = x + 3, [f, g] (4) yields (64, 7)
3. Apply-to-all:
A functional form that takes a single function as a parameter and yields a list of values obtained by
applying the given function to each element of a list of parameters.
Form:
For h (x) =x * x * x
f( h, (3, 2, 4)) yields (27, 8, 64)
LISP –
LISP is the first functional programming language; it contains two forms those are:
1. Data object types: originally only atoms and lists
2. List form: parenthesized collections of sub lists and/or atoms e.g.,(A B (C D) E)
Fundamentals of Functional Programming Languages:
The objective of the design of a FPL is to mimic mathematical functions to the greatest extent
possible. The basic process of computation is fundamentally different in a FPL than in an imperative
language
➢ In an imperative language, operations are done and the results are stored in variables for later use
➢ Management of variables is a constant concern and source of complexity for imperative
programming
➢ In an FPL, variables are not necessary, as is the case in mathematics
➢ In an FPL, the evaluation of a function always produces the same result given the same
parameters
➢ This is called referential transparency
A Bit of LISP:
– Originally, LISP was a type less language. There were only two data types, atom and list
– Lambda notation is used to specify functions and function definitions, function applications, and
data all have the same form.
E.g :,
If the list (A B C) is interpreted as data it is a simple list of three atoms, A, B, and C If it is
interpreted as a function application, it means that the function named A is applied to the two
parameters, B and C
-The first LISP interpreter appeared only as a demonstration of the universality of the computational
capabilities of the notation
Scheme:
– A mid-1970s dialect of LISP, designed to be cleaner, more modern, and simpler version than the
contemporary dialects of LISP, Uses only static scoping
– Functions are first-class entities, They can be the values of expressions and elements of lists,
Primitive Functions:
➢ Arithmetic: +, -, *, /, ABS, SQRTEx: (+ 5 2) yields 7
➢ QUOTE is required because the Scheme interpreter, named EVAL, always evaluates
parameters to function applications before applying the function. QUOTE is used to avoid parameter
evaluation when it is not appropriate
➢ QUOTE can be abbreviated with the apostrophe prefix operator e.g., '(A B) is equivalent to
(QUOTE (A B))
➢ CAR takes a list parameter; returns the first element of that list
– NULL? takes one parameter; it returns #Tif the parameter is the empty list; otherwise () Note that
NULL? returns #T if the parameter is ()
EX:
(DEFINE pi 3.141593) (DEFINE two_pi (* 2 pi))
To bind names to lambda expressions
EX:
(DEFINE (cube x) (* x x x))
Control Flow:
Selection-the special form, IF (IF predicate then_exp else_exp)
e.g.,
(IF (<> count 0) (/ sum count)0)
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 type less) and has no type coercions
➢ Includes exception handling and a module facility for implementing abstract data types
➢ Includes lists and list operations
➢ The val statement binds a name to a value (similar to DEFINE in Scheme)
➢ Function declaration form: fun function_name (formal_parameters)
Haskell
• Similar to ML (syntax, static scoped, strongly typed, type inferencing, pattern matching)
• Different from ML (and most other functional languages) in that it is purely functional (e.g., no
variables, no assignment statements, and no side effects of any kind).
• Syntax differences from ML
fact 0 = 1
fact n = n * fact (n – 1)
fib 0 = 1
fib 1 = 1
fib (n + 2) = fib (n + 1) + fib n
Function Definitions with Different Parameter Ranges
fact n
| n == 0 = 1
| n > 0 = n * fact(n – 1)
sub n
| n < 10 =0
| n > 100 =2
| otherwise =1
square x = x * x
Lazy Evaluation
• A language is strict if it requires all actual parameters to be fully evaluated
• A language is nonstrict if it does not have the strict requirement
• Nonstrict languages are more efficient and allow some interesting capabilities – infinite lists
• Lazy evaluation - Only compute those values that are necessary
• Positive numbers
positives = [0..]
• Determining if 16 is a square number
member [] b = False
member(a:x) b=(a == b)||member x b
squares = [n * n | n ← [0..]]
member squares 16
In such a system, the script is said to glue the sub systems together
COMMON CHARACTERISTICS OF SCRIPTING LANGUAGES
– Both batch and interactive use
– Economy of expressions
– Lack of declaration; simple scoping rules
– Flexible dynamic typing
– Easy access to other programs
– High level data types
– Glue other programs together
– Extensive text processing capabilities
– Portable across windows, unix ,mac
PYTHON
– PYTHON was designed in the early 1990s by Guido van Rossum.
– PYTHON borrows ideas from languages as diverse as PERL, HASKELL, and the object-oriented
languages, skillfully integrating these ideas into a coherent whole.
Python is extensible: if we invoke how to program in C, it is easy to add new built in functionor
module to the interpreter, either to perform critical operations at maximum speed of to link python
programs to libraries that may only be available in binary form.
– PYTHON has a rich repertoire of composite types: tuples, strings, lists, dictionaries, and objects.
– PYTHON adopts reference semantics. This is especially significant for mutable values, which can
be selectively updated.
Primitive values and strings are immutable; lists, dictionaries, and objects are mutable; tuples are
mutable if any of their components are mutable
– PYTHON‘s repertoire of commands include assignments, procedure calls, con-ditional (if-
butnotcase-) commands, iterative (while-and for-) commands, and exception-handling commands.
– Within a procedure we may initialize local variables and define local procedures.
– Within a class we may initialize variable components and define procedures (methods).
Procedural abstraction
The only difference is that a function procedure returns a value, while a proper procedure returns
nothing.
Since PYTHON is dynamically typed, a procedure definition states the name but not the type of each
formal parameter.
Python procedure
Eg :def gcd (m, n):
p,q=m,n
while p%q!=0:
p,q=q, p%q
return q
Data Abstraction
– PYTHON has three different constructs relevant to data abstraction: packages, modules, and
classes.
– Modules and classes support encapsulation, using a naming convention to distinguish between
public and private components.
– A Class is a group of components that may be class variables, class methods, and instance methods.
– A procedure defined in a class declaration acts as an instance method if its first formal parameter is
named self and refers to an object of the class being declared. Otherwise, the procedure acts as a
class method.
Separate Compilation
– PYTHON modules are compiled separately.
– Each module must explicitly import every other module on which it depends
– When that module is first imported, it is compiled, and its object code is stored in a file named
program.pyc
– The PYTHON compiler does not reject code that refers to undeclared identifiers. Such code simply
fails if and when it is executed.
– The compiler will not reject code that might fail with a type error, nor even code that will certainly
fail, such as:
Module Library
– PYTHON is equipped with a very rich module library, which supports string handling, markup,
mathematics, and cryptography, multimedia, GUIs, operating system services, internet services,
compilation, and so on.
– Unlike older scripting languages, PYTHON does not have built-in high-level string processing or
GUI support, so module library provides it