SlideShare a Scribd company logo
1
Elements of Functional Programming
2
A LITTLE LANGUAGE OF EXPRESSIONS
•The little language ----Little QuiltLittle Quilt:
•small enough to permit a short description
•different enough to require description
•representative enough to make description worthwhile
• Constructs in Little Quilt are expressions denoting
geometric objects call quilts:quilts:
3
A LITTLE LANGUAGE OF EXPRESSIONS
•What Does Little Quilt Manipulate?
•Little Quilt manipulates geometric objects with height,
width and texture
• Basic Value and Operations:
•The two primitive objects in the language are the
square piece.
The earliest programming languages
began with only integers and reals
4
A LITTLE LANGUAGE OF EXPRESSIONS
•The operation are specified by the following rules:
•A quilt is one of the primitive piece, or
•It is formed by turning a quilt clockwise 90°, or
•it is formed by sewing a quilt to the right of another
quilt of equal height.
•Nothing else is a quilt.
5
A LITTLE LANGUAGE OF EXPRESSIONS
• Constants:
• Names for basic values: the pieces be called aa and bb
•Names of operations: the operations be called turnturn and sewsew. (like
the picture on the previous slide)
•now that we have chosen the built-in object and operations (a,b, turn,
sew) expressions can be formed
•<expression>::=a | b | turn(<expression>)
• | sew (<expression>,<expression>)
6
TYPES: VALUES AND OPERATIONS
• A typetype consists of a set of elements called valuesvalues together
with a set of function called operationsoperations.
•<type-expression>::=<type-name>
| <type-expression> <type-expression>
| <type-expression>*<type-expression>
|<type-expression> list
•A type expression can be a type name, or it can denote a
function, product, or list type. (operations are ->, * and list)
7
TYPES: VALUES AND OPERATIONS
• Basic Types:
•A type is basic if its values are atomic
•the values are treated as whole elements, with no internal
structure.
•Example: the boolean values in the set { true, false}
•Operations on Basic Values:
• The only operation defined for all basic types is a
comparison for equality (have no internal structure)
8
TYPES: VALUES AND OPERATIONS
• Products of Types: The product A*B consists of ordered
pairs written as (a, b)
•Operations on Pairs
•A pair is constructed from a and b by writing (a, b)
•Associated with pairs are operations called projectionprojection
functionsfunctions to extract the first and second elements from a
pair
•Projection functionsProjection functions can be defined:
•fun first(x,y) = x;
•fun second( x, y)=y;
9
TYPES: VALUES AND OPERATIONS
• Lists of Elements:
•A list is a finite-length sequence of elements
•Type A list consists of all lists of elements, where each
element belongs to type A.
•Example:
•int list consists of all lists of integers
•[1,2,3] is a list of three integers 1, 2, and 3.
•[“red”, “white”, “blue”] is a list of three strings
10
TYPES: VALUES AND OPERATIONS
•Operations on Lists
•List-manipulation programs must be prepared to
construct and inspect lists of any length.
•Operations on list from ML:
•null(x) True if x is the empty list, false otherwise.
•hd(x) The first or head element of list x.
•tl(x) The tail or rest of the list after the first element
is removed.
•a::x Construct a list with head a and tail x.
Cons operator
11
TYPES: VALUES AND OPERATIONS
•Types in ML
Predeclared basic types of ML.
Type Name Values Operations
_____________________________________________________
boolean bool true,false =,<>,…
integer int …,-1,0,1,2,… =,<>,<,+,*,div,mod,…
real real …,0.0,…,3.14,.. =,<>,<,+,*,/,…
string string “foo”,””quoted”” =,<>,…
_____________________________________________________
•New basic types can be defined as needed by a datatype
declaration
•Example: datatype direction = ne | se |sw| nw;
12
FUNCTION DECLARATIONS
• Functions as Algorithms
A function declaration has three parts:
•The name of the declared function
•The parameters of the function
•A rule for computing a result from the parameters
13
FUNCTION DECLARATIONS
• Syntax of Function Declarations and Applications
•The basic syntax for function declarations is
fun <name><formal-parameter> = <body>
•<name> is the function name
•<formal-parameter> is a parameter name
•<body> is an expression to be evaluated
•fun successor n = n +1;
•fun successor (n)= n +1;
() are optional
14
FUNCTION DECLARATIONS
• The use of a function within an expression is called an
applicationapplication of the function.
•Prefix notation is the rule for the application of
declared function
•<name><actual-parameter>
•<name> is the function name
•<actual-parameter> is an expression
corresponding to the parameter name in the
declaration of the function
•Example: successor(2+3)
15
FUNCTION DECLARATIONS
• Recursive Functions -- A function f is recursive if its
body contains an application of f.
•Example1:
•Function len counts the number of elements in a list
•fun len(x)=
if null(x) then 0 else 1 + len(tl(x))
16
Approaches to Expression Evaluation
•Innermost Evaluation
•Outermost Evaluation
•Selective Evaluation
•Evaluation of Recursive Functions
•Short-Circuit Evaluation
17
Type Checking
Type Inference:
•Wherever possible ,ML infers the type of an expression.An error
is reported if the type of an expression cannot be inferred.
•If E and F have type int then E+F also has type int .
•In general,
If f is a function of type A -->B , and a has type A,
then f(a) has type B.
18
Type Names and Type Equivalence
Two type expressions are said to be structurally equivalent if and
only if they are equivalent under the following rules:
1. A type name is structurally equivalent to itself.
2. Two type expressions are structurally equivalent if they are
formed by
applying the same type constructor to structurally
equivalent types.
3. After a type declaration, type n = T ,the type name n is
structurally equivalent to T .
19
Overloading:Multiple Meanings
A symbol is said to be overloaded if it has different meanings in
different contexts.Family operator symbols like + and * are
overloaded.
e.g. 2+2 here + is of type int
2.5+3.6 here + is of type real.
ML cannot resolve overloading in fun add(x,y) = x+y ;
Explicit types can be used to resolve overloading.
fun add(x,y): int =x+y ;
PRESENTED BY
SAJJAD ALI P M
CS –B ,34
20
THANK YOU!!
21

More Related Content

What's hot (20)

PPTX
Basic data types in python
sunilchute1
 
PPTX
Values and Data types in python
Jothi Thilaga P
 
PPT
Getting started with c++
K Durga Prasad
 
PPTX
Programming construction tools
sunilchute1
 
PPT
Frequently asked questions in c
David Livingston J
 
PPTX
Chapter 2.datatypes and operators
Jasleen Kaur (Chandigarh University)
 
PPTX
LISP: Input And Output
DataminingTools Inc
 
PDF
Lec4slides
shawiz
 
PPT
Frequently asked questions in c
David Livingston J
 
PPTX
Input processing and output in Python
Raajendra M
 
PDF
Python revision tour II
Mr. Vikram Singh Slathia
 
PPT
Basic of c &c++
guptkashish
 
PPTX
C++
AL- AMIN
 
PPT
Token and operators
Samsil Arefin
 
PPTX
Python second ppt
RaginiJain21
 
PPTX
Lec9
kapil078
 
PPTX
Learn C LANGUAGE at ASIT
ASIT
 
PPT
M C6java2
mbruggen
 
PPT
Data types and Operators
raksharao
 
Basic data types in python
sunilchute1
 
Values and Data types in python
Jothi Thilaga P
 
Getting started with c++
K Durga Prasad
 
Programming construction tools
sunilchute1
 
Frequently asked questions in c
David Livingston J
 
Chapter 2.datatypes and operators
Jasleen Kaur (Chandigarh University)
 
LISP: Input And Output
DataminingTools Inc
 
Lec4slides
shawiz
 
Frequently asked questions in c
David Livingston J
 
Input processing and output in Python
Raajendra M
 
Python revision tour II
Mr. Vikram Singh Slathia
 
Basic of c &c++
guptkashish
 
Token and operators
Samsil Arefin
 
Python second ppt
RaginiJain21
 
Lec9
kapil078
 
Learn C LANGUAGE at ASIT
ASIT
 
M C6java2
mbruggen
 
Data types and Operators
raksharao
 

Viewers also liked (13)

PPTX
Object oriented-programming-vs-procedural-programming
kukurmutta
 
PPTX
Procedural vs. object oriented programming
Haris Bin Zahid
 
PPTX
Ak procedural vs oop
Abhishek Kumar
 
PPTX
diffrence between procedure oriented programming & object oriented programmin...
nihar joshi
 
PPTX
Prgramming paradigms
Anirudh Chauhan
 
KEY
Scala: functional programming for the imperative mind
Sander Mak (@Sander_Mak)
 
PDF
operating system structure
Waseem Ud Din Farooqui
 
PPTX
Functional Programming Fundamentals
Shahriar Hyder
 
PPT
Introduction to Procedural Programming in C++
Salahaddin University-Erbil
 
PPT
Oops ppt
abhayjuneja
 
PDF
Functional Programming Patterns (BuildStuff '14)
Scott Wlaschin
 
PPT
Object Oriented Programming Concepts
thinkphp
 
Object oriented-programming-vs-procedural-programming
kukurmutta
 
Procedural vs. object oriented programming
Haris Bin Zahid
 
Ak procedural vs oop
Abhishek Kumar
 
diffrence between procedure oriented programming & object oriented programmin...
nihar joshi
 
Prgramming paradigms
Anirudh Chauhan
 
Scala: functional programming for the imperative mind
Sander Mak (@Sander_Mak)
 
operating system structure
Waseem Ud Din Farooqui
 
Functional Programming Fundamentals
Shahriar Hyder
 
Introduction to Procedural Programming in C++
Salahaddin University-Erbil
 
Oops ppt
abhayjuneja
 
Functional Programming Patterns (BuildStuff '14)
Scott Wlaschin
 
Object Oriented Programming Concepts
thinkphp
 
Ad

Similar to Elements of functional programming (20)

PDF
15 functional programming
jigeno
 
PDF
15 functional programming
jigeno
 
PDF
Is there a perfect data-parallel programming language? (Experiments with More...
Julian Hyde
 
PPT
Functional Programming - Past, Present and Future
Pushkar Kulkarni
 
PPT
Functional Programming Past Present Future
IndicThreads
 
KEY
An Introduction to Functional Programming using Haskell
Michel Rijnders
 
PDF
iRODS Rule Language Cheat Sheet
Samuel Lampa
 
PDF
Rainer Grimm, “Functional Programming in C++11”
Platonov Sergey
 
PPT
ML: A Strongly Typed Functional Language
lijx127
 
PPTX
Scala Introduction
Constantine Nosovsky
 
PDF
Morel, a Functional Query Language
Julian Hyde
 
PDF
Functional programming ii
Prashant Kalkar
 
PDF
Introduction to functional programming (In Arabic)
Omar Abdelhafith
 
PDF
The Next Great Functional Programming Language
John De Goes
 
PPTX
Funtional Programming
Girish Khanzode
 
PDF
Scala Functional Patterns
league
 
PDF
Functional Programming #FTW
Adriano Bonat
 
PDF
ecoop-slides.pdf
AndongFAN
 
PPT
haskell5.ppt is a marketing document lol
dopointt
 
PDF
Functional programming from its fundamentals
Mauro Palsgraaf
 
15 functional programming
jigeno
 
15 functional programming
jigeno
 
Is there a perfect data-parallel programming language? (Experiments with More...
Julian Hyde
 
Functional Programming - Past, Present and Future
Pushkar Kulkarni
 
Functional Programming Past Present Future
IndicThreads
 
An Introduction to Functional Programming using Haskell
Michel Rijnders
 
iRODS Rule Language Cheat Sheet
Samuel Lampa
 
Rainer Grimm, “Functional Programming in C++11”
Platonov Sergey
 
ML: A Strongly Typed Functional Language
lijx127
 
Scala Introduction
Constantine Nosovsky
 
Morel, a Functional Query Language
Julian Hyde
 
Functional programming ii
Prashant Kalkar
 
Introduction to functional programming (In Arabic)
Omar Abdelhafith
 
The Next Great Functional Programming Language
John De Goes
 
Funtional Programming
Girish Khanzode
 
Scala Functional Patterns
league
 
Functional Programming #FTW
Adriano Bonat
 
ecoop-slides.pdf
AndongFAN
 
haskell5.ppt is a marketing document lol
dopointt
 
Functional programming from its fundamentals
Mauro Palsgraaf
 
Ad

Recently uploaded (20)

PDF
13th International Conference on Artificial Intelligence, Soft Computing (AIS...
ijait
 
PDF
PROGRAMMING REQUESTS/RESPONSES WITH GREATFREE IN THE CLOUD ENVIRONMENT
samueljackson3773
 
PDF
FSE-Journal-First-Automated code editing with search-generate-modify.pdf
cl144
 
PDF
Bayesian Learning - Naive Bayes Algorithm
Sharmila Chidaravalli
 
PPTX
FSE_LLM4SE1_A Tool for In-depth Analysis of Code Execution Reasoning of Large...
cl144
 
PPTX
Engineering Quiz ShowEngineering Quiz Show
CalvinLabial
 
PDF
輪読会資料_Miipher and Miipher2 .
NABLAS株式会社
 
PDF
13th International Conference of Security, Privacy and Trust Management (SPTM...
ijcisjournal
 
PDF
MODULE-5 notes [BCG402-CG&V] PART-B.pdf
Alvas Institute of Engineering and technology, Moodabidri
 
PPTX
Electrical_Safety_EMI_EMC_Presentation.pptx
drmaneharshalid
 
PDF
A Brief Introduction About Robert Paul Hardee
Robert Paul Hardee
 
DOCX
Engineering Geology Field Report to Malekhu .docx
justprashant567
 
PDF
CLIP_Internals_and_Architecture.pdf sdvsdv sdv
JoseLuisCahuanaRamos3
 
PDF
Decision support system in machine learning models for a face recognition-bas...
TELKOMNIKA JOURNAL
 
PPTX
Unit_I Functional Units, Instruction Sets.pptx
logaprakash9
 
PPTX
Stability of IBR Dominated Grids - IEEE PEDG 2025 - short.pptx
ssuser307730
 
PDF
Artificial Neural Network-Types,Perceptron,Problems
Sharmila Chidaravalli
 
PPTX
Comparison of Flexible and Rigid Pavements in Bangladesh
Arifur Rahman
 
PDF
Module - 5 Machine Learning-22ISE62.pdf
Dr. Shivashankar
 
PDF
Module - 4 Machine Learning -22ISE62.pdf
Dr. Shivashankar
 
13th International Conference on Artificial Intelligence, Soft Computing (AIS...
ijait
 
PROGRAMMING REQUESTS/RESPONSES WITH GREATFREE IN THE CLOUD ENVIRONMENT
samueljackson3773
 
FSE-Journal-First-Automated code editing with search-generate-modify.pdf
cl144
 
Bayesian Learning - Naive Bayes Algorithm
Sharmila Chidaravalli
 
FSE_LLM4SE1_A Tool for In-depth Analysis of Code Execution Reasoning of Large...
cl144
 
Engineering Quiz ShowEngineering Quiz Show
CalvinLabial
 
輪読会資料_Miipher and Miipher2 .
NABLAS株式会社
 
13th International Conference of Security, Privacy and Trust Management (SPTM...
ijcisjournal
 
MODULE-5 notes [BCG402-CG&V] PART-B.pdf
Alvas Institute of Engineering and technology, Moodabidri
 
Electrical_Safety_EMI_EMC_Presentation.pptx
drmaneharshalid
 
A Brief Introduction About Robert Paul Hardee
Robert Paul Hardee
 
Engineering Geology Field Report to Malekhu .docx
justprashant567
 
CLIP_Internals_and_Architecture.pdf sdvsdv sdv
JoseLuisCahuanaRamos3
 
Decision support system in machine learning models for a face recognition-bas...
TELKOMNIKA JOURNAL
 
Unit_I Functional Units, Instruction Sets.pptx
logaprakash9
 
Stability of IBR Dominated Grids - IEEE PEDG 2025 - short.pptx
ssuser307730
 
Artificial Neural Network-Types,Perceptron,Problems
Sharmila Chidaravalli
 
Comparison of Flexible and Rigid Pavements in Bangladesh
Arifur Rahman
 
Module - 5 Machine Learning-22ISE62.pdf
Dr. Shivashankar
 
Module - 4 Machine Learning -22ISE62.pdf
Dr. Shivashankar
 

Elements of functional programming

  • 2. 2 A LITTLE LANGUAGE OF EXPRESSIONS •The little language ----Little QuiltLittle Quilt: •small enough to permit a short description •different enough to require description •representative enough to make description worthwhile • Constructs in Little Quilt are expressions denoting geometric objects call quilts:quilts:
  • 3. 3 A LITTLE LANGUAGE OF EXPRESSIONS •What Does Little Quilt Manipulate? •Little Quilt manipulates geometric objects with height, width and texture • Basic Value and Operations: •The two primitive objects in the language are the square piece. The earliest programming languages began with only integers and reals
  • 4. 4 A LITTLE LANGUAGE OF EXPRESSIONS •The operation are specified by the following rules: •A quilt is one of the primitive piece, or •It is formed by turning a quilt clockwise 90°, or •it is formed by sewing a quilt to the right of another quilt of equal height. •Nothing else is a quilt.
  • 5. 5 A LITTLE LANGUAGE OF EXPRESSIONS • Constants: • Names for basic values: the pieces be called aa and bb •Names of operations: the operations be called turnturn and sewsew. (like the picture on the previous slide) •now that we have chosen the built-in object and operations (a,b, turn, sew) expressions can be formed •<expression>::=a | b | turn(<expression>) • | sew (<expression>,<expression>)
  • 6. 6 TYPES: VALUES AND OPERATIONS • A typetype consists of a set of elements called valuesvalues together with a set of function called operationsoperations. •<type-expression>::=<type-name> | <type-expression> <type-expression> | <type-expression>*<type-expression> |<type-expression> list •A type expression can be a type name, or it can denote a function, product, or list type. (operations are ->, * and list)
  • 7. 7 TYPES: VALUES AND OPERATIONS • Basic Types: •A type is basic if its values are atomic •the values are treated as whole elements, with no internal structure. •Example: the boolean values in the set { true, false} •Operations on Basic Values: • The only operation defined for all basic types is a comparison for equality (have no internal structure)
  • 8. 8 TYPES: VALUES AND OPERATIONS • Products of Types: The product A*B consists of ordered pairs written as (a, b) •Operations on Pairs •A pair is constructed from a and b by writing (a, b) •Associated with pairs are operations called projectionprojection functionsfunctions to extract the first and second elements from a pair •Projection functionsProjection functions can be defined: •fun first(x,y) = x; •fun second( x, y)=y;
  • 9. 9 TYPES: VALUES AND OPERATIONS • Lists of Elements: •A list is a finite-length sequence of elements •Type A list consists of all lists of elements, where each element belongs to type A. •Example: •int list consists of all lists of integers •[1,2,3] is a list of three integers 1, 2, and 3. •[“red”, “white”, “blue”] is a list of three strings
  • 10. 10 TYPES: VALUES AND OPERATIONS •Operations on Lists •List-manipulation programs must be prepared to construct and inspect lists of any length. •Operations on list from ML: •null(x) True if x is the empty list, false otherwise. •hd(x) The first or head element of list x. •tl(x) The tail or rest of the list after the first element is removed. •a::x Construct a list with head a and tail x. Cons operator
  • 11. 11 TYPES: VALUES AND OPERATIONS •Types in ML Predeclared basic types of ML. Type Name Values Operations _____________________________________________________ boolean bool true,false =,<>,… integer int …,-1,0,1,2,… =,<>,<,+,*,div,mod,… real real …,0.0,…,3.14,.. =,<>,<,+,*,/,… string string “foo”,””quoted”” =,<>,… _____________________________________________________ •New basic types can be defined as needed by a datatype declaration •Example: datatype direction = ne | se |sw| nw;
  • 12. 12 FUNCTION DECLARATIONS • Functions as Algorithms A function declaration has three parts: •The name of the declared function •The parameters of the function •A rule for computing a result from the parameters
  • 13. 13 FUNCTION DECLARATIONS • Syntax of Function Declarations and Applications •The basic syntax for function declarations is fun <name><formal-parameter> = <body> •<name> is the function name •<formal-parameter> is a parameter name •<body> is an expression to be evaluated •fun successor n = n +1; •fun successor (n)= n +1; () are optional
  • 14. 14 FUNCTION DECLARATIONS • The use of a function within an expression is called an applicationapplication of the function. •Prefix notation is the rule for the application of declared function •<name><actual-parameter> •<name> is the function name •<actual-parameter> is an expression corresponding to the parameter name in the declaration of the function •Example: successor(2+3)
  • 15. 15 FUNCTION DECLARATIONS • Recursive Functions -- A function f is recursive if its body contains an application of f. •Example1: •Function len counts the number of elements in a list •fun len(x)= if null(x) then 0 else 1 + len(tl(x))
  • 16. 16 Approaches to Expression Evaluation •Innermost Evaluation •Outermost Evaluation •Selective Evaluation •Evaluation of Recursive Functions •Short-Circuit Evaluation
  • 17. 17 Type Checking Type Inference: •Wherever possible ,ML infers the type of an expression.An error is reported if the type of an expression cannot be inferred. •If E and F have type int then E+F also has type int . •In general, If f is a function of type A -->B , and a has type A, then f(a) has type B.
  • 18. 18 Type Names and Type Equivalence Two type expressions are said to be structurally equivalent if and only if they are equivalent under the following rules: 1. A type name is structurally equivalent to itself. 2. Two type expressions are structurally equivalent if they are formed by applying the same type constructor to structurally equivalent types. 3. After a type declaration, type n = T ,the type name n is structurally equivalent to T .
  • 19. 19 Overloading:Multiple Meanings A symbol is said to be overloaded if it has different meanings in different contexts.Family operator symbols like + and * are overloaded. e.g. 2+2 here + is of type int 2.5+3.6 here + is of type real. ML cannot resolve overloading in fun add(x,y) = x+y ; Explicit types can be used to resolve overloading. fun add(x,y): int =x+y ;
  • 20. PRESENTED BY SAJJAD ALI P M CS –B ,34 20