SlideShare a Scribd company logo
ICS 313 - Fundamentals of Programming Languages 1
15. Functional Programming
15.1 Introduction
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
ICS 313 - Fundamentals of Programming Languages 2
15.2 Mathematical 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
for the function cube (x) = x * x * x
Lambda expressions describe nameless functions
Lambda expressions are applied to parameter(s) by placing
the parameter(s) after the expression
e.g. (λ(x) x * x * x)(3)
which evaluates to 27
15.2 Mathematical Functions (continued)
Functional Forms
A higher-order function, or functional form, is one
that either takes functions as parameters or yields a
function as its result, or both
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))
ICS 313 - Fundamentals of Programming Languages 3
15.2 Mathematical Functions (continued)
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)
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
α ( h, (3, 2, 4)) yields (27, 8, 64)
15.3 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
ICS 313 - Fundamentals of Programming Languages 4
Functional Programming Languages
LISP
Lambda notation is used to specify functions and function definitions, function
applications, and data all have the same form
Scheme
A mid-1970s dialect of LISP, designed to be a cleaner, more modern, and simpler
version than the contemporary dialects of LISP
COMMON LISP
A combination of many of the features of the popular dialects of LISP around in the
early 1980s
ML
A static-scoped functional language with syntax that is closer to Pascal than to LISP
Haskell
Similar to ML (syntax, static scoped, strongly typed, type inferencing)
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)
15.8 Haskell
Most Important Features
Uses lazy evaluation (evaluate no subexpression until the value
is needed)
Has “list comprehensions,” which allow it to deal with infinite
lists
Examples
Fibonacci numbers (illustrates function definitions with different
parameter forms)
fib 0 = 1
fib 1 = 1
fib (n + 2) = fib (n + 1) + fib n
ICS 313 - Fundamentals of Programming Languages 5
15.8 Haskell (continued)
Factorial (illustrates guards)
fact n
| n == 0 = 1
| n > 0 = n * fact (n - 1)
The special word otherwise can appear as a guard
List operations
List notation: Put elements in brackets
e.g., directions = [north, south, east, west]
Length: #
e.g., #directions is 4
Arithmetic series with the .. operator
e.g., [2, 4..10] is [2, 4, 6, 8, 10]
Catenation is with ++
e.g., [1, 3] ++ [5, 7] results in [1, 3, 5, 7]
CAR and CDR via the colon operator (as in Prolog)
e.g., 1:[3, 5, 7] results in [1, 3, 5, 7]
15.8 Haskell (continued)
Examples:
product [] = 1
product (a:x) = a * product x
fact n = product [1..n]
List comprehensions: set notation
e.g.,
[n * n | n ← [1..20]]
defines a list of the squares of the first 20 positive integers
factors n = [i | i [1..n div 2],n mod i == 0]
This function computes all of the factors of its given parameter
Quicksort:
sort [] = []
sort (a:x) = sort [b | b ← x; b <= a]
++ [a] ++
sort [b | b ← x; b > a]
ICS 313 - Fundamentals of Programming Languages 6
15.8 Haskell (continued)
Lazy evaluation
Infinite lists
e.g.,
positives = [0..]
squares = [n * n | n ← [0..]]
(only compute those that are necessary)
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 b
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
15.8 Haskell (continued)
Applications of Functional Languages:
LISP is used for artificial intelligence
Knowledge representation
Machine learning
Natural language processing
Modeling of speech and vision
Scheme is used to teach introductory programming at a significant
number of universities
Comparing Functional and Imperative Languages
Imperative Languages:
Efficient execution
Complex semantics
Complex syntax
Concurrency is programmer designed
Functional Languages:
Simple semantics
Simple syntax
Inefficient execution
Programs can automatically be made concurrent

More Related Content

What's hot (20)

PPTX
Basic operators in matlab
rishiteta
 
PPTX
Introduction to matlab lecture 1 of 4
Randa Elanwar
 
PDF
Matlab from Beginner to Expert
smart-ideas
 
PPTX
Matlab Workshop Presentation
Jairo Maldonado-Contreras
 
PDF
Introduction to Matlab
Amr Rashed
 
PPT
Matlab practical and lab session
Dr. Krishna Mohbey
 
PDF
Matlab intro
Chaitanya Banoth
 
PPT
Introduction to MatLab programming
Damian T. Gordon
 
PPT
Matlab Overviiew
Nazim Naeem
 
DOCX
MATLAB BASICS
butest
 
PPTX
MATLAB - The Need to Know Basics
STEM Course Prep
 
PPTX
Intro to Matlab programming
Ahmed Moawad
 
PPTX
Matlab 1(operations on_matrix)
harman kaur
 
PPTX
Ppt 2 d ploting k10998
Vinit Rajput
 
PPTX
All About MATLAB
Multisoft Virtual Academy
 
PDF
Introduction to MATLAB
Sarah Hussein
 
PDF
Matlab solved problems
Make Mannan
 
PPT
Matlab intro
THEMASTERBLASTERSVID
 
PPSX
Matlab basic and image
Divyanshu Rasauria
 
PPT
Introduction to matlab
Mohan Raj
 
Basic operators in matlab
rishiteta
 
Introduction to matlab lecture 1 of 4
Randa Elanwar
 
Matlab from Beginner to Expert
smart-ideas
 
Matlab Workshop Presentation
Jairo Maldonado-Contreras
 
Introduction to Matlab
Amr Rashed
 
Matlab practical and lab session
Dr. Krishna Mohbey
 
Matlab intro
Chaitanya Banoth
 
Introduction to MatLab programming
Damian T. Gordon
 
Matlab Overviiew
Nazim Naeem
 
MATLAB BASICS
butest
 
MATLAB - The Need to Know Basics
STEM Course Prep
 
Intro to Matlab programming
Ahmed Moawad
 
Matlab 1(operations on_matrix)
harman kaur
 
Ppt 2 d ploting k10998
Vinit Rajput
 
All About MATLAB
Multisoft Virtual Academy
 
Introduction to MATLAB
Sarah Hussein
 
Matlab solved problems
Make Mannan
 
Matlab intro
THEMASTERBLASTERSVID
 
Matlab basic and image
Divyanshu Rasauria
 
Introduction to matlab
Mohan Raj
 

Viewers also liked (11)

PDF
9 subprograms
jigeno
 
PDF
14 exception handling
jigeno
 
PDF
6 data types
jigeno
 
PDF
15 functional programming
jigeno
 
PDF
Access2007 m2
jigeno
 
PDF
8 statement-level control structure
jigeno
 
PDF
12 object oriented programming
jigeno
 
PDF
7 expressions and assignment statements
jigeno
 
PDF
1 preliminaries
jigeno
 
PDF
4 lexical and syntax analysis
jigeno
 
PPT
Unit 3 principles of programming language
Vasavi College of Engg
 
9 subprograms
jigeno
 
14 exception handling
jigeno
 
6 data types
jigeno
 
15 functional programming
jigeno
 
Access2007 m2
jigeno
 
8 statement-level control structure
jigeno
 
12 object oriented programming
jigeno
 
7 expressions and assignment statements
jigeno
 
1 preliminaries
jigeno
 
4 lexical and syntax analysis
jigeno
 
Unit 3 principles of programming language
Vasavi College of Engg
 
Ad

Similar to 15 functional programming (20)

PPTX
Funtional Programming
Girish Khanzode
 
PDF
Functional Programming #FTW
Adriano Bonat
 
PPT
Elements of functional programming
Sajjad Ali Pulikkanat
 
PPT
Functional Programming Languages slidesslies.ppt
BikalAdhikari4
 
PDF
Introduction to functional programming (In Arabic)
Omar Abdelhafith
 
PPS
Presentation of GetTogether on Functional Programming
Filip De Sutter
 
PPTX
Plc part 3
Taymoor Nazmy
 
PPT
Functional Programming - Past, Present and Future
Pushkar Kulkarni
 
PPT
Functional Programming Past Present Future
IndicThreads
 
PDF
Functional programming using haskell notes iitk
benevolent001
 
PDF
Rainer Grimm, “Functional Programming in C++11”
Platonov Sergey
 
PDF
Clqr a4-consec
Benjamin Brown
 
PDF
Functional programming
ijcd
 
KEY
An Introduction to Functional Programming using Haskell
Michel Rijnders
 
PPTX
Can programming be liberated from the von neumann style?
Oriol López Massaguer
 
PDF
[FLOLAC'14][scm] Functional Programming Using Haskell
Functional Thursday
 
PDF
Clojure intro
Basav Nagur
 
PDF
Data Structures Chapter-2
priyavanimurugarajan
 
PPTX
Functional programming
Heman Gandhi
 
PPT
functional programming language of pcpf subject
Jaya Chavan
 
Funtional Programming
Girish Khanzode
 
Functional Programming #FTW
Adriano Bonat
 
Elements of functional programming
Sajjad Ali Pulikkanat
 
Functional Programming Languages slidesslies.ppt
BikalAdhikari4
 
Introduction to functional programming (In Arabic)
Omar Abdelhafith
 
Presentation of GetTogether on Functional Programming
Filip De Sutter
 
Plc part 3
Taymoor Nazmy
 
Functional Programming - Past, Present and Future
Pushkar Kulkarni
 
Functional Programming Past Present Future
IndicThreads
 
Functional programming using haskell notes iitk
benevolent001
 
Rainer Grimm, “Functional Programming in C++11”
Platonov Sergey
 
Clqr a4-consec
Benjamin Brown
 
Functional programming
ijcd
 
An Introduction to Functional Programming using Haskell
Michel Rijnders
 
Can programming be liberated from the von neumann style?
Oriol López Massaguer
 
[FLOLAC'14][scm] Functional Programming Using Haskell
Functional Thursday
 
Clojure intro
Basav Nagur
 
Data Structures Chapter-2
priyavanimurugarajan
 
Functional programming
Heman Gandhi
 
functional programming language of pcpf subject
Jaya Chavan
 
Ad

More from jigeno (10)

PDF
Access2007 part1
jigeno
 
PDF
Basic introduction to ms access
jigeno
 
PPSX
Bsit1
jigeno
 
PDF
16 logical programming
jigeno
 
PDF
13 concurrency
jigeno
 
PDF
11 abstract data types
jigeno
 
PDF
5 names
jigeno
 
PDF
3 describing syntax and semantics
jigeno
 
PDF
2 evolution of the major programming languages
jigeno
 
PDF
Access2007 m1
jigeno
 
Access2007 part1
jigeno
 
Basic introduction to ms access
jigeno
 
Bsit1
jigeno
 
16 logical programming
jigeno
 
13 concurrency
jigeno
 
11 abstract data types
jigeno
 
5 names
jigeno
 
3 describing syntax and semantics
jigeno
 
2 evolution of the major programming languages
jigeno
 
Access2007 m1
jigeno
 

Recently uploaded (20)

PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
PDF
Advancing WebDriver BiDi support in WebKit
Igalia
 
PDF
July Patch Tuesday
Ivanti
 
PDF
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
PDF
IoT-Powered Industrial Transformation – Smart Manufacturing to Connected Heal...
Rejig Digital
 
PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
PDF
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
PPTX
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
PDF
Biography of Daniel Podor.pdf
Daniel Podor
 
PPTX
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
PDF
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
PPTX
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
PDF
What Makes Contify’s News API Stand Out: Key Features at a Glance
Contify
 
PDF
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PPTX
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PPTX
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
PDF
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
PDF
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
PDF
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
Advancing WebDriver BiDi support in WebKit
Igalia
 
July Patch Tuesday
Ivanti
 
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
IoT-Powered Industrial Transformation – Smart Manufacturing to Connected Heal...
Rejig Digital
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
Biography of Daniel Podor.pdf
Daniel Podor
 
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
What Makes Contify’s News API Stand Out: Key Features at a Glance
Contify
 
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 

15 functional programming

  • 1. ICS 313 - Fundamentals of Programming Languages 1 15. Functional Programming 15.1 Introduction 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
  • 2. ICS 313 - Fundamentals of Programming Languages 2 15.2 Mathematical 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 for the function cube (x) = x * x * x Lambda expressions describe nameless functions Lambda expressions are applied to parameter(s) by placing the parameter(s) after the expression e.g. (λ(x) x * x * x)(3) which evaluates to 27 15.2 Mathematical Functions (continued) Functional Forms A higher-order function, or functional form, is one that either takes functions as parameters or yields a function as its result, or both 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))
  • 3. ICS 313 - Fundamentals of Programming Languages 3 15.2 Mathematical Functions (continued) 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) 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 α ( h, (3, 2, 4)) yields (27, 8, 64) 15.3 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
  • 4. ICS 313 - Fundamentals of Programming Languages 4 Functional Programming Languages LISP Lambda notation is used to specify functions and function definitions, function applications, and data all have the same form Scheme A mid-1970s dialect of LISP, designed to be a cleaner, more modern, and simpler version than the contemporary dialects of LISP COMMON LISP A combination of many of the features of the popular dialects of LISP around in the early 1980s ML A static-scoped functional language with syntax that is closer to Pascal than to LISP Haskell Similar to ML (syntax, static scoped, strongly typed, type inferencing) 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) 15.8 Haskell Most Important Features Uses lazy evaluation (evaluate no subexpression until the value is needed) Has “list comprehensions,” which allow it to deal with infinite lists Examples Fibonacci numbers (illustrates function definitions with different parameter forms) fib 0 = 1 fib 1 = 1 fib (n + 2) = fib (n + 1) + fib n
  • 5. ICS 313 - Fundamentals of Programming Languages 5 15.8 Haskell (continued) Factorial (illustrates guards) fact n | n == 0 = 1 | n > 0 = n * fact (n - 1) The special word otherwise can appear as a guard List operations List notation: Put elements in brackets e.g., directions = [north, south, east, west] Length: # e.g., #directions is 4 Arithmetic series with the .. operator e.g., [2, 4..10] is [2, 4, 6, 8, 10] Catenation is with ++ e.g., [1, 3] ++ [5, 7] results in [1, 3, 5, 7] CAR and CDR via the colon operator (as in Prolog) e.g., 1:[3, 5, 7] results in [1, 3, 5, 7] 15.8 Haskell (continued) Examples: product [] = 1 product (a:x) = a * product x fact n = product [1..n] List comprehensions: set notation e.g., [n * n | n ← [1..20]] defines a list of the squares of the first 20 positive integers factors n = [i | i [1..n div 2],n mod i == 0] This function computes all of the factors of its given parameter Quicksort: sort [] = [] sort (a:x) = sort [b | b ← x; b <= a] ++ [a] ++ sort [b | b ← x; b > a]
  • 6. ICS 313 - Fundamentals of Programming Languages 6 15.8 Haskell (continued) Lazy evaluation Infinite lists e.g., positives = [0..] squares = [n * n | n ← [0..]] (only compute those that are necessary) 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 b 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 15.8 Haskell (continued) Applications of Functional Languages: LISP is used for artificial intelligence Knowledge representation Machine learning Natural language processing Modeling of speech and vision Scheme is used to teach introductory programming at a significant number of universities Comparing Functional and Imperative Languages Imperative Languages: Efficient execution Complex semantics Complex syntax Concurrency is programmer designed Functional Languages: Simple semantics Simple syntax Inefficient execution Programs can automatically be made concurrent