Ppt Functional Programming
Ppt Functional Programming
Functional Programming
Stephen M. Watt
University of Western Ontario
Functional Programming
• Some believe the need to use concurrency for future hardware speed-up as
“the end of the free lunch” and see FP as the solution.
• Advocates say
• FP here stands for “functional programming”, but is also the name of a particular
functional programming language by John Backus, of Fortran fame.
Related Concepts
• When you do this some things get easier and some things get harder.
Java Man
Pithecanthropus erectus
from “The Outline of Science” J. Arthur Thomson (1922)
Elements of Scheme
• Syntax: (operator arg ...)
• Some operators are built-in, others programmer defined.
• define: introduce a name (valid at top level and certain other places)
(define n 7)
List Operations
Recursive Structures
• With recursive list data structures, it is natural to write
recursive programs.
Local Bindings
• E.g.
(define factorial (lambda (n)
(let ((nm1 (- n 1)))
(if (< n 2)
1
(* n (factorial nm1)) ) ) ))
Lexical Scoping
• An inner function use all the local names of the functions that
enclose it.
(inner-fn (+ n 2) ) ))
• E.g.
• E.g. A counter...
• Functional composition
• E.g.
((plus 5) 4) ; Yields 9
(minus1 9) ; Yields 8
• Very powerful
• Complex ideas can be expressed with short programs
• E.g.
(define make-five (lambda () (write “Hello”) (+ 2 3)))