KEMBAR78
Introduction to functional programming (In Arabic) | PDF
Intro to functional
programming
whoami
Hello!
I am Omar Abdelhafith from Jordan
I live in Dublin
I write blogs on http://nsomar.com
I work with computers @Zendesk
I write mainly Swift, Haskell and Elixir
On twitter @ifnottrue
On github @oarrabi
I organize Swift and Elixir meetups (in Dublin)
We are hiring
http://bit.ly/zendesk-ios
Intro to functional
programming
Intro to functional programming
• What topics to discuss?
• lambda calculus
• practical category theory
• functors, monad, monoids
• promises, futures, next
• frp, reactive
• fp langs
Functional
programming
First item: Intro to functional
programming
To start: Intro to functional
programming
• History
• Definition
• Some Examples
• PS: This is an interactive session
Note: Languages used
• Java
• Swift
• Haskell
• Pseudo language
Let's start
Functional programming
Functional Programming (FP):
Is a declarative programming paradigm, where programs are
built around mathematical functions and immutable data
transformation.
^ That is my
definition
FP is a very wide field
The definition can vary from
lenient to rigorous
Lenient Definition
Graham Hutton:
Functional programming is style of programming in which the
basic method of computation is the application of functions to
arguments;
• No mention of immutability
In-the-middle Definition
From Haskell wiki:
In functional programming, programs are executed by
evaluating expressions, ... Functional programming typically
avoids using mutable state.
... (it) requires that functions are first-class, ...(to end of quote)
• Mathematical functions is implied
Super Rigorus Definition
Well, Not really a definition. But,
Standarized ladder of functional programming (Lambda Conf)
Some history
Lambda calculus
• Alonzo Church In 1930 invented λ-calculus
• Calculation is done by lambda abstraction (definition)
and binding (application)
• There are no assignments of variables
• John McCarthy created LISP in the 1950s
• Inspired from lambda calculus
• LISP is the first FP lang
• Just a bit after Fortran
• Introduced lots of concepts (macros, tree ds)
• Still alive today (Common Lisp, Dialects, etc..)
• Created by an International comittee in 1987
• Pure functional
• lazy
• in 90s they implemented ideas from Category Theory
• functors, monads, monoids
Today
Today
• JVM: Scala, Clojure
• Web: Clojure Script, ELM
• BEAM VM: Erlang, Elixir
• LLVM: Swift
• General purpose: Idris
Recap?
Is a declarative programming paradigm, where programs are
built around mathematical functions and immutable data
transformation.
Decompose
• Declarative programming paradigm
• Mathematical functions
• Immutable data
Decompose
• Declarative programming paradigm
• Mathematical functions
• Immutable data
• Everything is an expression
• Functions are data
Programming paradigms
• Imperative (C, C++, Java, Go):
• state and control (if, for, while)
• How to do
• Declarative (Haskell, SQL)
• logic but no control
• What to do
Imperative Programming
Paradigm
• Tell the computer how to do things
• Change state
• Based on statment
Imperative Programming
Paradigm
• Procedural (Go, C):
• functions and data
• Object Oriented Programming (Java, C++, C#):
• objects with behaviour and state
• Tell dont ask
Declarative Programming
Paradigm
• Tell the computer what to do
• High level
• Less control
Declarative Programming
Paradigm
• Data base query
• Regex
• DSLs
• Functional programming
Examples
SQL
Regex
More complex regex
DSL
Functional
Another FP example
Decompose
• Declarative programming paradigm ✅
• Mathematical functions
• Immutable data
• Everything is an expression
• Functions are data
Mathematical functions
• They are pure
• They are Total
• They are composable
Pure functions
• Given same input, return same output
• They dont cause side effects
Pure functions: Another
definition
Functions that you can memoize (memoization)
-- Bartosz Milewski
A bit offtopic - Memoization
• Technique to speedup programms by storing results
• Similar to caching but low level
• Tabulate
Memoization Example: Fibonacci
Without memoization
• fib(n: 10) -> 88 times
• fib(n: 11) -> 143 times
Memoization Example: Fibonacci
With memoization
• fib(n: 10) -> 9 times
• fib(n: 11) -> 10 times
Pure functions:
functions that can be
memoized
Pure functions
• sin(x) ❓
• length(s) ❓
• add(n1,n2) ❓
• random() ❓
• printf("Hello World") ❓
Pure functions
• sin(x) ✅
• length(s) ❓
• add(n1,n2) ❓
• random() ❓
• printf("Hello World") ❓
Pure functions
• sin(x) ✅
• length(s) ✅
• add(n1,n2) ❓
• random() ❓
• printf("Hello World") ❓
Pure functions
• sin(x) ✅
• length(s) ✅
• add(n1,n2) ✅
• random() ❓
• printf("Hello World") ❓
Pure functions
• sin(x) ✅
• length(s) ✅
• add(n1,n2) ✅
• random() ❌
• printf("Hello World") ❓
Pure functions
• sin(x) ✅
• length(s) ✅
• add(n1,n2) ✅
• random() ❌ but random(seed) ✅
• printf("Hello World") ❓
Pure functions
• sin(x) ✅
• length(s) ✅
• add(n1,n2) ✅
• random() ❌ but random(seed) ✅
• printf("Hello World") ❌
Mathematical functions
• They are pure ✅
• They are Total
• They are composable
Total functions
• For all set of inputs return always an output
• add :: (Int, Int) -> Int
Examples
• add(int, int), sub(int, int) ❓
• mul(int, int), div(int, int) ❓
• is_even(int) ❓
• int parseInt(String s) throws
NumberFormatException ❓
• FILE *fopen(const char *filename, ...) ❓
Examples
• add(int, int), sub(int, int) ✅
• mul(int, int), div(int, int) ❓
• int parseInt(String s) throws
NumberFormatException ❓
• FILE *fopen(const char *filename, ...) ❓
Examples
• add(int, int), sub(int, int) ✅
• mul(int, int) ✅, div(int, int) ❌
• int parseInt(String s) throws
NumberFormatException ❓
• FILE *fopen(const char *filename, ...) ❓
Examples
• add(int, int), sub(int, int) ✅
• mul(int, int) ✅, div(int, int) ❌
• int parseInt(String s) throws
NumberFormatException ❌
• FILE *fopen(const char *filename, ...) ❓
Examples
• add(int, int), sub(int, int) ✅
• mul(int, int) ✅, div(int, int) ❌
• int parseInt(String s) throws
NumberFormatException ❌
• FILE *fopen(const char *filename, ...) ❌
Mathematical functions
• They are pure ✅
• They are Total ✅
• They are composable
Function composition
• f :: a -> b
• g :: b -> c
• g(f(a))
• f o g :: a -> c
Function composition
• multiplyBy2 :: Int -> Int
• convertToString :: Int -> String
• x = multiplyBy2(2)
• convertToString(x)
• convertToString(multiplyBy2(2))
• (convertToString . multiplyBy2)(2)
Example in FP language
Let's delay it after high order functions
Decompose
• Declarative programming paradigm ✅
• Mathematical functions ✅
• Immutable data
• Everything is an expression
• Functions are data
Immutable data
• Data does not change
• it gets copied when passed
• In FP, all data is immutable
What about
performance
No problem with "Copy on write"
Decompose
• Declarative programming paradigm ✅
• Mathematical functions ✅
• Immutable data ✅
• Everything is an expression
• Functions are data
Everything is an expression
• Statments: tell how to do
• Expression: return things
Statements
Expressions
Everything is an expression
• Statments: tell how to do
• Expression: return things
• In FP: There is no void
• In Haskell ---> absurd :: Void -> a
Decompose
• Declarative programming paradigm ✅
• Mathematical functions ✅
• Immutable data ✅
• Everything is an expression ✅
• Functions are data
Functions are data
• In FP functions are data
• Higher-order function
• They can be passed to functions
• They can be returned from functions
They can be passed as
params
They can be returned
Composed: Passed
and returned
Decompose
• Declarative programming paradigm ✅
• Mathematical functions ✅
• Immutable data ✅
• Everything is an expression ✅
• Functions are data ✅
Recap
• Declarative: What to do and not how to do it
• Mathematical functions: Pure, Total, Composable
• Immutable data: Dont change the state
• Everything is an expression: and they are composable
• Functions are data: Can be passed and returned from
functions
Why are we seeing a rise in FP
• Multi core and paralellism
• Determinism, testability
• They align well with cloud computing and microservices
Questions
Thanks

Introduction to functional programming (In Arabic)