SlideShare a Scribd company logo
Why FP
Elements of Functional languages
Type inference
•

Simplicity

•

Readability

•

Code economy
C/C++ example
int factorial(int n) {
return !n ? 1 : n * factorial(n - 1);
}
Haskell example
factorial :: Integer -> Integer
factorial n
| n < 0 = error «not defined»
| n == 0 = 1
| otherwise = n * fac (n - 1)
Haskell example
factorial n
| n == 0 = 1
| otherwise = n * fac (n - 1)
Haskell example

factorial n = product [1..n]
Higher-order function
(HOF)
Javascript example
_.sortBy([5,4,1,6], function(n){
return Math.sin(n)
})
Closure
Common Lisp example
(define counter-from
(lambda (n)
(lambda ()
(set! n (+ n 1))
n)))
Partial application
Python example
def partial(func, *args, **keywords):
def newfunc(*fargs, **fkeywords):
newkeywords = keywords.copy()
newkeywords.update(fkeywords)
return func(*(args + fargs),
**newkeywords)
newfunc.func = func
newfunc.args = args
newfunc.keywords = keywords
return newfunc
Immutable & persistent
data structures
Why fp
ADT
OOP example

abstract class Shape {
abstract float area();
abstract void draw();
}
class Rectangle extends Shape {
float w, h;
...
float area() { return w * h; }
}
class Circle extends Shape {
float r;
...
float area() { return PI * r * r; }
}
ADT example
data Shape =
Rectangle Float Float
| Circle Float
area :: Shape -> Float
area (Rectangle w h) = w * h
area (Circle r) = pi * r * r
draw :: Shape -> IO ()
...
Compare
Rectangle

Circle

Other

area()

ADT

draw()

other()

OOP
Pattern matching
Haskell example
head (x:_) = x
tail (_:xs)= xs
Chain of responsibility
Abstract example
[recieve,
check,
process,
finish]

More Related Content

PDF
The Scheme Language -- Using it on the iPhone
PPTX
Scheme language
PPTX
Scheme Programming Language
PDF
Introduction to functional programming (In Arabic)
ODP
Introduction to Programming in LISP
PPTX
PDF
Functional Python Webinar from October 22nd, 2014
PPTX
Functions in C++ (OOP)
The Scheme Language -- Using it on the iPhone
Scheme language
Scheme Programming Language
Introduction to functional programming (In Arabic)
Introduction to Programming in LISP
Functional Python Webinar from October 22nd, 2014
Functions in C++ (OOP)

What's hot (19)

PPT
INTRODUCTION TO LISP
PPT
Advance LISP (Artificial Intelligence)
PPT
An Overview Of Python With Functional Programming
PDF
Introduction to functional programming
PPTX
LISP: Introduction to lisp
PPT
(Ai lisp)
PPTX
A brief introduction to lisp language
PPTX
PPTX
Function overloading
PDF
Functional programing in Javascript (lite intro)
PDF
CSEG1001 Unit 4 Functions and Pointers
PDF
(chapter 7) A Concise and Practical Introduction to Programming Algorithms in...
PDF
Thinking in Functions: Functional Programming in Python
PDF
CSEG1001Unit 3 Arrays and Strings
PDF
Intro to functional programming
PDF
Python functional programming
PDF
Functional Programming in JavaScript
PPTX
Functions in c++
PPTX
Csc1100 lecture02 ch02-datatype_declaration
INTRODUCTION TO LISP
Advance LISP (Artificial Intelligence)
An Overview Of Python With Functional Programming
Introduction to functional programming
LISP: Introduction to lisp
(Ai lisp)
A brief introduction to lisp language
Function overloading
Functional programing in Javascript (lite intro)
CSEG1001 Unit 4 Functions and Pointers
(chapter 7) A Concise and Practical Introduction to Programming Algorithms in...
Thinking in Functions: Functional Programming in Python
CSEG1001Unit 3 Arrays and Strings
Intro to functional programming
Python functional programming
Functional Programming in JavaScript
Functions in c++
Csc1100 lecture02 ch02-datatype_declaration
Ad

Similar to Why fp (20)

PPTX
Functional pogramming hl overview
PPTX
Good functional programming is good programming
PDF
haskell_fp1
PPTX
An Introduction to Functional Programming with Javascript
PDF
Functional Programming in Go
PPTX
Functional programming
PDF
Fp for the oo programmer
PPTX
Functional programming for the Advanced Beginner
PDF
On fuctional programming, high order functions, ML
PDF
Rainer Grimm, “Functional Programming in C++11”
PDF
A taste of Functional Programming
PDF
OOP and FP
PPTX
Functional Programming Fundamentals
PDF
Functional programming-advantages
PPTX
PDF
Functional Programming #FTW
PDF
Arriving at monads by going from pure-function composition to effectful-funct...
PPTX
Столпы функционального программирования для адептов ООП, Николай Мозговой
PDF
Functional programming
PDF
Demystify Functional Programming in Swift
Functional pogramming hl overview
Good functional programming is good programming
haskell_fp1
An Introduction to Functional Programming with Javascript
Functional Programming in Go
Functional programming
Fp for the oo programmer
Functional programming for the Advanced Beginner
On fuctional programming, high order functions, ML
Rainer Grimm, “Functional Programming in C++11”
A taste of Functional Programming
OOP and FP
Functional Programming Fundamentals
Functional programming-advantages
Functional Programming #FTW
Arriving at monads by going from pure-function composition to effectful-funct...
Столпы функционального программирования для адептов ООП, Николай Мозговой
Functional programming
Demystify Functional Programming in Swift
Ad

More from Антон Плешивцев (10)

PDF
How to create modern web application (about Virool mobile player)
PDF
CodeFest dirty facts about AngularJS
PDF
Повышение конверсии через оптимизацию JS
PDF
Frontend for the win
PDF
как мы сделали поисковой движок
PDF
Happydev presentation soa
PDF
Happydev presentation angular
PPT
интерфейсы3 ppt
How to create modern web application (about Virool mobile player)
CodeFest dirty facts about AngularJS
Повышение конверсии через оптимизацию JS
Frontend for the win
как мы сделали поисковой движок
Happydev presentation soa
Happydev presentation angular
интерфейсы3 ppt

Why fp