Functional Programming
Paul Vorobyev and Heman Gandhi
What is a programming paradigm?
A way of approaching code, data, and design.
Imperative is thinking of code as blocks that run from top to bottom.
Data is manipulated by storing and altering values.
Object oriented is thinking of code as an interaction between objects
Objects have behavior and data
Event-driven
The code responds to events and data changes as events happen
The Functional Paradigm
Code is a series of functions. Invocations of
functions are the key operation.
Functions are data types as well, so they can
be accepted as parameters and returned
from other functions (there will be
examples).
Data is always immutable - nothing can be
changed, merely copied.
Why?
Easy to debug.
Elegant.
Concurrent.
Some History on Paradigms
Mathematicians tried to denote everything they could compute and people came
up with their own systems.
Turing came up with the universal Turing machine.
Church helped develop lambda calculus.
Curry helped with combinatory logic.
The universal Turing machine happens to be the easiest to implement with
current hardware.
Lambda Calculus
This is the mathematical foundation of functional programming.
The entirety is two things:
Calling functions (with 1 argument)
Defining functions (with 1 argument)
This is Turing complete.
𝜆x.x // This is the identity function
𝜆x.𝜆y.(y x) //Call the function “y” on some variable “x”.
Alonzo Church
Developed lambda calculus in the 1930s.
Turing and Church proved that a Turing machine and lambda calculus are
equivalent.
The famous “Church-Turing thesis”
Numbers in lambda calculus are “Church numerals”.
𝜆f.𝜆x.x = 0 (and false)
𝜆n.𝜆f.𝜆x.((n f) x) = succ n = n + 1
Haskell Curry
Made Combinatory Logic (similar to lambda calculus).
Has Haskell named after him.
Currying is also named after him (but he didn’t make it). Currying lets you make
n-argument functions from 1-argument functions.
𝜆f.𝜆x.x = 𝜆f x.x //This is well-defined by Currying.
The outer lambdas return a function and the last one returns a value.
((𝜆n m.(n + m)) 4 5) = ((𝜆n.𝜆m.(n + m) 4) 5) = (𝜆m.(4 + m) 5) = 4 + 5 = 9
Examples: Into the Clojure
The syntax of this language is very simple (non-existent).
(<thing being called> <val1> … <val n>)
This is a mean: “(/ (+ x y) 2)”
This is the quadratic formula: (the colorful parens are called rainbow parens)
(/ (+ (* -1 b) (Math/sqrt (- (* b b) (* 4 a c)))) (* 2 a))
This is a simple checker for evenness:
(if (= 0 (mod x 2)) true false) http://www.tryclj.com/
RECURSION!!!!!
Higher order
Closures
Doin’ it live!
Newton’s method solver given an error bound, a function and a function that is
its derivative.
Make it return a function that accepts a starting point.
Google Searches
Languages
Haskell
Scheme
F#
Common Lisp
OCamL
Clojure
Other languages also have good support:
C#, C++, Java
Concepts
Streams (or generators, iterators)
Variable capture
Monads
Functors

Functional programming

  • 1.
  • 2.
    What is aprogramming paradigm? A way of approaching code, data, and design. Imperative is thinking of code as blocks that run from top to bottom. Data is manipulated by storing and altering values. Object oriented is thinking of code as an interaction between objects Objects have behavior and data Event-driven The code responds to events and data changes as events happen
  • 3.
    The Functional Paradigm Codeis a series of functions. Invocations of functions are the key operation. Functions are data types as well, so they can be accepted as parameters and returned from other functions (there will be examples). Data is always immutable - nothing can be changed, merely copied. Why? Easy to debug. Elegant. Concurrent.
  • 4.
    Some History onParadigms Mathematicians tried to denote everything they could compute and people came up with their own systems. Turing came up with the universal Turing machine. Church helped develop lambda calculus. Curry helped with combinatory logic. The universal Turing machine happens to be the easiest to implement with current hardware.
  • 5.
    Lambda Calculus This isthe mathematical foundation of functional programming. The entirety is two things: Calling functions (with 1 argument) Defining functions (with 1 argument) This is Turing complete. 𝜆x.x // This is the identity function 𝜆x.𝜆y.(y x) //Call the function “y” on some variable “x”.
  • 6.
    Alonzo Church Developed lambdacalculus in the 1930s. Turing and Church proved that a Turing machine and lambda calculus are equivalent. The famous “Church-Turing thesis” Numbers in lambda calculus are “Church numerals”. 𝜆f.𝜆x.x = 0 (and false) 𝜆n.𝜆f.𝜆x.((n f) x) = succ n = n + 1
  • 7.
    Haskell Curry Made CombinatoryLogic (similar to lambda calculus). Has Haskell named after him. Currying is also named after him (but he didn’t make it). Currying lets you make n-argument functions from 1-argument functions. 𝜆f.𝜆x.x = 𝜆f x.x //This is well-defined by Currying. The outer lambdas return a function and the last one returns a value. ((𝜆n m.(n + m)) 4 5) = ((𝜆n.𝜆m.(n + m) 4) 5) = (𝜆m.(4 + m) 5) = 4 + 5 = 9
  • 8.
    Examples: Into theClojure The syntax of this language is very simple (non-existent). (<thing being called> <val1> … <val n>) This is a mean: “(/ (+ x y) 2)” This is the quadratic formula: (the colorful parens are called rainbow parens) (/ (+ (* -1 b) (Math/sqrt (- (* b b) (* 4 a c)))) (* 2 a)) This is a simple checker for evenness: (if (= 0 (mod x 2)) true false) http://www.tryclj.com/
  • 9.
  • 10.
  • 11.
  • 12.
    Doin’ it live! Newton’smethod solver given an error bound, a function and a function that is its derivative. Make it return a function that accepts a starting point.
  • 13.
    Google Searches Languages Haskell Scheme F# Common Lisp OCamL Clojure Otherlanguages also have good support: C#, C++, Java Concepts Streams (or generators, iterators) Variable capture Monads Functors