PPL Unit - V
PPL Unit - V
PPL Unit - V
Unit - V
Functional, Logic and
Scripting programming Languages
Functional Programming Languages: Introduction,
Mathematical Functions, Fundamentals of Functional
Programming Language, LISP, Support for Functional
Programming in Primarily Imperative Languages,
Comparison of Functional and Imperative Languages
Logic Programming Language: Introduction, an Overview
of Logic Programming, Basic Elements of Prolog,
Applications of Logic Programming.
Scripting Language: Pragmatics, Key Concepts, Case
Study: Python – Values and Types, Variables, Storage
and Control, Bindings and Scope, Procedural
Abstraction, Data Abstraction, Separate Compilation,
Types of Programming
Languages
1. Procedural programming languages
4. Scripting languages
Source:
https://www.coursera.org/articles/types-programming-language
Source:
Source:
https://www.educba.com/types-of-computer-language/
Functional Programming
Language
The design of the imperative languages is based directly on
the von Neumann architecture
Efficiency is the primary concern, rather than the
suitability of the language for software development
The design of the functional languages is based
on mathematical functions
A solid theoretical basis that is also closer to the user, but
relatively unconcerned with the architecture of the
machines on which programs will run
Mathematical Functions - Simple
Functions
A mathematical function is a mapping of members of one
set, called the domain set, to another set, called the range
set
A lambda expression specifies the parameter(s) and the
mapping of a function in the following form
λ(x) x * x * x
(A (B C) D (E (F G)))
is a list of four elements. The first is the atom A; the
second is the sublist (B C); the third is the atom D; the
Internal Representation of LISP
List
Internal Representation of LISP
List
LISP
Example
Function calls were specified in a prefix list form
originally called
Cambridge Polish
(+ 5 7)
LISP
Source:
Comparison of Imperative and Functional
Languages
Source:
Logic Programming
Language
Logic Programming
Language
Programming that uses a form of symbolic logic as
a programming language is often called logic
programming
Languages based on symbolic logic are called logic
programming languages, or declarative languages.
Logic Programming
Language
The approach is to express programs in a form of
symbolic logic and use a logical inferencing process to
produce results.
Programs in logic programming languages
are collections of facts and rules.
Logic programming language - Prolog,
because it is the
only widely used logic language
Logic Programming
Language
The syntax of logic programming languages is
remarkably different from that of the imperative and
functional languages.
The semantics of logic programs also bears little
resemblance to that of imperative-language programs.
These observations should lead the reader to
some curiosity about the nature of logic
programming and declarative languages.
Logic Programming
Language
Languages used for logic programming are called
declarative languages, because programs written in
them consist of declarations rather than assignments
and control flow statements.
These declarations are actually statements, or
propositions, in symbolic logic.
Logic Programming
Language
One of the essential characteristics of logic
programming languages is their semantics, which
is called declarative semantics.
Declarative semantics is considerably simpler
than the semantics of the imperative languages.
For example, the meaning of a given proposition in a
logic programming language can be concisely
determined from the statement itself.
Logic Programming
Language
Programming in both imperative and functional
languages is primarily procedural, which means that
the programmer knows what is to be accomplished by a
program and instructs the computer on exactly how the
computation is to be done.
Programming in a logic programming language is
nonprocedural. Programs in such languages do not state
exactly how a result is to be computed but rather
describe the form of the result.
Logic Programming
Language
An example commonly used to illustrate the difference
between procedural and nonprocedural systems is sorting.
In a language like Java, sorting is done by explaining in
a Java program all of the details of some sorting
algorithm to a computer that has a Java compiler.
The computer, after translating the Java program
into machine code or some interpretive intermediate
code, follows the instructions and produces the
sorted list.
Logic Programming
Language
In a nonprocedural language, it is necessary only to
describe the characteristics of the sorted list:
It is some permutation of the given list such that for
each pair of adjacent elements, a given relationship
holds between the two elements.
To state this formally, suppose the list to be sorted is in
an array named list that has a subscript range 1 . . . n.
Logic Programming
Language
The concept of sorting the elements of the
given list, named old_list, and placing them in
a separate array, named new_list, can then
be expressed as follows:
The Basic Elements of
Prolog
The Basic Elements of
Prolog
There are now a number of different dialects of Prolog.
interpret
source code output
Hello.py
Features of Python:
Easy-to-learn: Python has relatively few keywords, simple
structure, and a clearly defined syntax.
Easy-to-read: Python code is much more clearly defined and
visible to the eyes.
Easy-to-maintain: Python's success is that its source code is
fairly easy-to-maintain.
A broad standard library: One of Python's greatest strengths is
the bulk of the library is very portable and cross-platform
compatible on UNIX, Windows, and Macintosh.
Interactive Mode: Support for an interactive mode in which you
can enter results from a terminal right to the language, allowing
interactive testing and debugging of snippets of code.
Portable: Python can run on a wide variety of hardware platforms
and has the same interface on all platforms.
Extendable: You can add low-level modules to the Python
interpreter. These modules enable programmers to add to or
customize their tools to be more efficient.
Databases: Python provides interfaces to all major commercial
databases.
GUI Programming: Python supports GUI applications that can
be created and ported to many system calls, libraries, and windows
systems, such as Windows MFC, Macintosh, and the X Window
system of Unix.
Scalable: Python provides a better structure and support for large
programs than shell scripting.
Script Mode Programming :
Invoking the interpreter with a script parameter begins
execution of the script and continues until the script is finished.
When the script is finished, the interpreter is no longer active.
Output:
Hello World!
H
llo
llo World!
Hello World!Hello World!
Hello World!TEST
List in python:
Lists are the most versatile of Python's compound data types. A list
contains items separated by commas and enclosed within square
brackets ([]).
To some extent, lists are similar to arrays in C. One difference
between them is that all the items belonging to a list can be of
different data type.
The values stored in a list can be accessed using the slice operator
( [ ] and [ : ] ) with indexes starting at 0 in the beginning of the list
and working their way to end-1.
The plus ( + ) sign is the list concatenation operator, and the asterisk
( * ) is the repetition operator.
list = [ 'abcd', 786 , 2.23, 'john', 70.2 ]
tinylist = [123, 'john']
The main differences between lists and tuples are: Lists are enclosed
in brackets ( [ ] ), and their elements and size can be changed, while
tuples are enclosed in parentheses ( ( ) ) and cannot be updated.
Tuples can be thought of as read-only lists.
Example:
tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 )
tinytuple = (123, 'john‘)
Dictionary in Python:
Python 's dictionaries are hash table type. They work like
associative arrays or hashes found in Perl and consist of key-value
pairs.
Keys can be almost any Python type, but are usually numbers or
strings. Values, on the other hand, can be any arbitrary Python
object.
Dictionaries are enclosed by curly braces ( { } ) and values can be
assigned and accessed using square braces ( [] ).
dict = {}
dict['one'] = "This is one"
dict[2] = "This is two“
tinydict = {'name': 'john','code':6734, 'dept': 'sales'}
print dict['one'] # Prints value for 'one' key
print dict[2] # Prints value for 2 key
print tinydict # Prints complete dictionary
print tinydict.keys() # Prints all the keys
print tinydict.values() # Prints all the values
OUTPUT:
This is one
This is two
{'dept': 'sales', 'code': 6734, 'name': 'john'}
['dept', 'code', 'name']
['sales', 6734, 'john‘]
SCOPE OF VARIABLE:
The scope of a variable determines the portion of the program
where you can access a particular identifier.
There are two basic scopes of variables in Python:
Global variables
Local variables
Global vs. Local variables:
Scope of a variable is the region in the code where the variable is
available/accessible.
A variable declared outside a function (i.e. the main region of the
code) is called a global variable.
a variable declared inside a function is called a local variable of
that function.
When you call a function, the variables declared inside it are
brought into scope.
Example:
total = 0; # This is global variable.
def sum( arg1, arg2 ):
"Add both the parameters"
total = arg1 + arg2;
print "Inside the function local total : ", total ;
return total;
# Now you can call sum function
sum( 10, 20 );
print "Outside the function global total : ", total;
This would produce following result:
Inside the function local total : 30
Outside the function global total : 0
# Uses global because there is no local 'a'
a=1
def f():
print('Inside f() : ', a)
# Variable 'a' is redefined as a local
def g():
a=2
print('Inside g() : ', a)
# Uses global keyword to modify global 'a'
def h():
global a
a=3
print('Inside h() : ', a)
# Global scope
print('global : ', a)
f()
print('global : ', a)
g()
print('global : ', a)
h()
print('global : ', a)
Define Abstraction.:
an abstraction is used to hide the irrelevant data/class in order to
reduce the complexity.
It also enhances the application efficiency.
Procedural abstraction:
Procedural abstraction is when we write code sections (called
"procedures" or in Java, "static methods") which are generalized by
having variable parameters.
The idea is that we have code which can cope with a variety of
different situations, depending on how its parameters are set when it
is called.
Example: In Python when we write from math import log; print log(2) we really
don't care about how log function is implemented in math library.
Data abstraction:
Data abstraction is one of the most essential concepts of Python ,which is
used to hide irrelevant details from the user and show the details that are
relevant to the users.