functional programming {week 12}
DESCRIPTION
The College of Saint Rose CIS 433 – Programming Languages David Goldschmidt, Ph.D. Functional programming {week 12}. from Concepts of Programming Languages , 9th edition by Robert W. Sebesta, Addison-Wesley, 2010, ISBN 0-13-607347-6. Von Neumann architecture. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Functional programming {week 12}](https://reader036.vdocument.in/reader036/viewer/2022062520/56816049550346895dcf6fea/html5/thumbnails/1.jpg)
Functional programming{week 12}
The College of Saint RoseCIS 433 – Programming LanguagesDavid Goldschmidt, Ph.D.
from Concepts of Programming Languages, 9th edition by Robert W. Sebesta, Addison-Wesley, 2010, ISBN 0-13-607347-6
![Page 2: Functional programming {week 12}](https://reader036.vdocument.in/reader036/viewer/2022062520/56816049550346895dcf6fea/html5/thumbnails/2.jpg)
Von Neumann architecture Imperative Languages
Data and programs are both stored in memory
Variables mimic memory Assignment statements Arithmetic operations Iterative repetition Control structures etc.
![Page 3: Functional programming {week 12}](https://reader036.vdocument.in/reader036/viewer/2022062520/56816049550346895dcf6fea/html5/thumbnails/3.jpg)
History
![Page 4: Functional programming {week 12}](https://reader036.vdocument.in/reader036/viewer/2022062520/56816049550346895dcf6fea/html5/thumbnails/4.jpg)
Functional language design The design of imperative languages is based
directly on the von Neumann architecture Efficiency is a primary concern Variables are abstractions of memory locations
The design of functional languages is based directly on mathematical functions A solid theoretical basis more natural to users Minimally concerned with machine architecture
![Page 5: Functional programming {week 12}](https://reader036.vdocument.in/reader036/viewer/2022062520/56816049550346895dcf6fea/html5/thumbnails/5.jpg)
Mathematical functions (i) A mathematical function is a
mapping ofmembers from one set (the domain) tomembers of another set (the range) Parameters represent any
member of the domain Once set, a parameter is
fixed to represent exactly one value during the evaluation of the mapping expression
# Pythondef cube( x ): return x * x * x
![Page 6: Functional programming {week 12}](https://reader036.vdocument.in/reader036/viewer/2022062520/56816049550346895dcf6fea/html5/thumbnails/6.jpg)
Mathematical functions (ii) Mathematical functions define values
whereas typical programming language functions produce values
cube(x) ≡ x * x * x, where x is a real number
function namefunction parameter(s)
“is defined as” mapping expression
![Page 7: Functional programming {week 12}](https://reader036.vdocument.in/reader036/viewer/2022062520/56816049550346895dcf6fea/html5/thumbnails/7.jpg)
Lambda expressions (i) A lambda expression specifies the
parameters and mapping expression of a function Essentially a nameless function
During evaluation, parameter x is bound to a particular member of the domain
(x)x * x * x
function parameter(s)
# Pythonlambda x:x*x*x
mapping expression
![Page 8: Functional programming {week 12}](https://reader036.vdocument.in/reader036/viewer/2022062520/56816049550346895dcf6fea/html5/thumbnails/8.jpg)
Lambda expressions (ii)
The lambda expression is the function itself Apply the expression to one or more
parameters((x)x * x * x)(4)
# Python(lambda x:x*x*x)(4)
((x,y)x * y)(8,7)
# Python(lambda x,y:x*y)(8,7)
![Page 9: Functional programming {week 12}](https://reader036.vdocument.in/reader036/viewer/2022062520/56816049550346895dcf6fea/html5/thumbnails/9.jpg)
Functional forms
A functional form is a higher-order function that either takes functions as parameters oryields functions as its results (or both) Example: the apply-to-all (α) functional
form
cube(x) ≡ x * x * x, where x is a real number
α( cube, (3, 5, 2) ) ===> (27, 125, 8)
# Pythonmap( cube, [3, 5, 2] )map( math.sqrt, [2, 3, 4, 5] )map( lambda x,y:x+y, [3, 4, 5], [6, 7, 8] )
![Page 10: Functional programming {week 12}](https://reader036.vdocument.in/reader036/viewer/2022062520/56816049550346895dcf6fea/html5/thumbnails/10.jpg)
Functional languages (i)
In imperative languages,operations are performedand results are stored invariables for later use Management of variables is a constant
concern and source of complexity (and bugs!)
Functions in imperative languagehave side effects
![Page 11: Functional programming {week 12}](https://reader036.vdocument.in/reader036/viewer/2022062520/56816049550346895dcf6fea/html5/thumbnails/11.jpg)
Functional languages (ii)
Functional programming languages mimicmathematical functions and mappings to the extent possible The basic process of computation
is fundamentally different than inimperative languages
No flow of control No variables
![Page 12: Functional programming {week 12}](https://reader036.vdocument.in/reader036/viewer/2022062520/56816049550346895dcf6fea/html5/thumbnails/12.jpg)
Who needs variables?
Write a program to calculate n-factorial The input argument n is your only
variablefactorial(n) ≡1 if n = 0
n x factorial(n – 1) if n > 0{
![Page 13: Functional programming {week 12}](https://reader036.vdocument.in/reader036/viewer/2022062520/56816049550346895dcf6fea/html5/thumbnails/13.jpg)
LISP (LISt Processing)
LISP is a functional language designedat MIT by John McCarthy in 1958 Research in artificial intelligence
required a language to:▪ Process data in dynamic lists▪ Support symbolic computation
(rather than numeric)
![Page 14: Functional programming {week 12}](https://reader036.vdocument.in/reader036/viewer/2022062520/56816049550346895dcf6fea/html5/thumbnails/14.jpg)
LISP data structures
LISP has two data structures: Atom: either a symbol or a numeric
literal List: a sequence of atoms and/or lists(A B C D)
(A (B C) D (E (F G)))NIL
![Page 15: Functional programming {week 12}](https://reader036.vdocument.in/reader036/viewer/2022062520/56816049550346895dcf6fea/html5/thumbnails/15.jpg)
Scheme
Scheme was developed at MIT in the 1970s to be a cleaner and simpler version of LISP Scheme uses an interpreter and built-in
IDE Literals evaluate to themselves
Scheme is available as Racketat http://racket-lang.org
![Page 16: Functional programming {week 12}](https://reader036.vdocument.in/reader036/viewer/2022062520/56816049550346895dcf6fea/html5/thumbnails/16.jpg)
What next?
Read and study Chapter 15
Download Racket Do Exercises at the
end of Chapter 15