Functional Programming
Bas Bossink
April 2010
Contents
Introduction
Definition
History
Taxonomy
Features
Functional concepts
When to use it?
When not to use it?
Resources
Introduction
Disclaimer
Inventory
Anecdote
About
GFDL
on github
libre tools
Definition
Functions are central construct
Functions are regular values
passed around
returned
composed
(partially) applied
No variables but binding of values
all variables are immutable
Definition of a Function
Functions are mathematical
Example
Figure 1:
Non-example
Figure 2:
Definition of a Function
Same input -> same output
No side effects, pure
History
Lambda calculus
Alonzo Church 1930s
untyped lambda calculus
simply typed lambda calculus
Figure 3:
Taxonomy
Group functional languages across several axis:
Statically typed / Dynamically typed
Strong typing / Weak typing
Lazy / Eager
Single paradigm / Multi paradigm
Taxonomy
Taxonomy
Features
Read Eval Print Loop (REPL)
Type inference (var on steroids)
Pattern matching
Algabraic data types
Function composition
Anonymous functions (lambda expressions)
Closure
Features
Sequence Epressions (List Comprehensions)
Partial function application (currying)
Higher order functions (map, fold, filter)
Funtional concepts
Tail Recursion
Continuation passing style
Memoization
Computation Expressions (Monads)
Concept from mathematics, Category theory
A triple:
type constructor
bind aka chain operation
return aka inject operation
Category
a collection of elements
a collection of morphisms (think function)
a notion of composition of these morphisms
Example Grp, Set, Hask
Functor
Transformation between categories
Given category C, D, functor F
F : C -> D
f: A -> B then F(f) -> F(A) -> F(B)
Examples: F: Grp -> Set, forgetful functor
Monad
Functor: M: C -> C
unit: X -> M(X)
join: M(M(X)) -> M(X)
Monads
class Monad m where
(>>=) :: m a -> (a -> m b) -> m b
return :: a -> m a
instance Monad [] where
return x = [x]
xs >>= f = concat (map f xs)
When to use it?
stateless
data-transformations
calculation /scientific computing
parrallellism
When not to use it?
a lotta state
gui’s
Resources
F#
Expert F#
Wikibook
Developer Center
HubFs
Haskell
Real World Haskell (book)
Real World Haskell online
Haskell.org
Haskell lectures Erik Meijer
Yet another Haskell tutorial
Labs
Haskell
F#
fix code such that quickcheck/fscheck succeeds
solutions on solution branch
try euler project

Functional programming