PPL Unit-5 Notes1
PPL Unit-5 Notes1
asia
UNIT-V
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
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
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
- LISP lists are stored internally as single-linked lists
- 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
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
- Functions are first-class entities, They can be the values of expressions and elements of lists, They can
be assigned to variables and passed as parameters
- Primitive Functions:
1. Arithmetic: +, -, *, /, ABS, SQRT
Ex: (+ 5 2) yields 7
2. QUOTE: -takes one parameter; returns the parameter without evaluation
- 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))
3. CAR takes a list parameter; returns the first element of that list
e.g., (CAR '(A B C)) yields A
(CAR '((A B) C D)) yields (A B)
4. CDR takes a list parameter; returns the list after removing its first element
e.g., (CDR '(A B C)) yields (B C)
(CDR '((A B) C D)) yields (C D)
5. CONS takes two parameters, the first of which can be either an atom or a list and the second of
which is a list; returns a new list that includes the first parameter as its first element and the second
parameter as the remainder of its result
e.g., (CONS 'A '(B C)) returns (A B C)
6. LIST - takes any number of parameters; returns a list with the parameters as elements
- Predicate Functions: (#T and () are true and false)
1. EQ? takes two symbolic parameters; it returns #T if both parameters are atoms and the two are the
same
e.g.,(EQ? 'A 'A) yields #T
(EQ? 'A '(A B)) yields ()
Note that if EQ? is called with list parameters, the result is not reliable Also, EQ? does not work for
numeric atoms
2. LIST? takes one parameter; it returns #T if the parameter is an list; otherwise ()
3. NULL? takes one parameter; it returns #T if the parameter is the empty list; otherwise ()
Note that NULL? returns #T if the parameter is ()
4. Numeric Predicate Functions
=, <>, >, <, >=, <=, EVEN?, ODD?, ZERO?
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
e.g.,
((LAMBDA (L) (CAR (CAR L))) '((A B) C D))
- A Function for Constructing Functions
DEFINE - Two forms:
1. To bind a symbol to an expression
EX:
(DEFINE pi 3.141593)
(DEFINE two_pi (* 2 pi))
2. To bind names to lambda expressions
EX:
(DEFINE (cube x) (* x x x))
- Example use:
(cube 4)
- Evaluation process (for normal functions):
1. Parameters are evaluated, in no particular order
2. The values of the parameters are substituted into the function body
3. The function body is evaluated
4. The value of the last expression in the body is the value of the function
(Special forms use a different evaluation process)
Control Flow:
- 1. Selection- the special form, IF
(IF predicate then_exp else_exp)
e.g.,
(IF (<> count 0)
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
(/ 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
(See Chapter 4)
- 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
Haskell:
- Different from ML (and most other functional languages) in that it is PURELY functional
Examples
fib 0 = 1
fib 1 = 1
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
fact n
| n == 0 = 1
| n > 0 = n * fact (n - 1)
a guard
3. List operations
- Length: #
e.g., #directions is 4
- Catenation is with +
[1, 3, 5, 7]
- Examples:
product [ ] = 1
e.g.,
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
[n * n | n ¬ [1..20]]
positive integers
n mod i == 0]
Quicksort:
sort [ ] = [ ]
++ [a] ++
sort [b | b ¬ x; b > a]
5. Lazy evaluation
- Infinite lists
e.g.,
positives = [0..]
squares = [n * n | n ¬ [0..]]
e.g.,
member squares 16 would return True ,The member function could be written as: member
[] b = False member (a:x) b = (a == b) || member x
However, this would only work if the parameter to squares was a perfect square; if not, it will keep
generating them forever. The following version will always work:
member2 (m:x) n
|m<n = member2 x n
| m == n = True
| otherwise = False
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
Imperative Languages:
o Efficient execution
o Complex semantics
o Complex syntax
- Functional Languages:
o Simple semantics
o Simple syntax
o Inefficient execution
Scripting languages
Pragmatics
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
In such a system, the script is said to glue the sub systems together
PYTHON
Python is extensible: if we invoke how to program in C, it is easy to add new built in function or 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
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
PYTHON has a limited repertoire of primitive types: integer, real, and complex
Numbers.
It has no specific character type; single-character strings are used instead.
Its boolean values (named False and True) are just small integers.
PYTHON has a rich repertoire of composite types: tuples, strings, lists, dictionaries, and
objects.
Primitive values and strings are immutable; lists, dictionaries, and objects are mutable; tuples are mutable
if any of their components are mutable
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
Python is a dynamically typed language. Based on the value, type of the variable is during the execution
of the program.
Python (dynamic)
C=1
C = [1,2,3]
C(static)
Double c; c = 5.2;
C = “a string….”
Weakly vs. strongly typed python language differs in their automatic conversions.
Perl (weak)
$b = `1.2`
$c = 5 * $b;
Python (strong)
b =`1.2`
c= 5* b;
• A PYTHON program consists of a number of modules, which may be grouped into packages.
• Within a module we may initialize variables, define procedures, and declare classes
• Within a procedure we may initialize local variables and define local procedures.
• Within a class we may initialize variable components and define procedures (methods).
• PYTHON was originally a dynamically-scoped language, but it is now statically scoped
In python, variables defined inside the function are local to that function. In order to change them as
global variables, they must be declared as global inside the function as given below.
S=1
Def myfunc(x,y);
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
Z=0
Global s;
S=2
Procedural abstraction
Since PYTHON is dynamically typed, a procedure definition states the name but not the type of each
formal parameter
Python procedure
p,q=m,n
while p%q!=0:
p,q=q,p%q
return q
min = val
max = val
Data Abstraction
• PYTHON has three different constructs relevant to data abstraction: packages ,modules , and
classes
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
• 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
• 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.
jntuworldupdates.org Specworld.in