ºÝºÝߣ

ºÝºÝߣShare a Scribd company logo
Functional
Programming
with Haskell
Adriano Bonat
abonat@thoughtworks.com
@tanob
Why FP?
? Source of new ideas
? Expressiveness
? Multi-core CPUs
? Different paradigm
New ideas:
Garbage collection (LISP)
Type inference (simply
typed lambda calculus)
Generics
Type classes
Expressiveness:
DSLs
What is it?
? Different programming paradigm
? OO
? Logic
? Procedural
? Functions are the main element in the
language
Function applications
¡°Functional programming is so called because a
program consists entirely of functions. [...]
Typically the main function is de?ned in terms of other
functions, which in turn are de?ned in terms of still
more functions, until at the bottom level the functions
are language primitives.¡±
John Hughes, 1989 -Why functional programming matters
Origin
Alonzo Church developed Lambda
Calculus as part of his
investigations on Math foundations
on 1936.
Lambda Calculus
? Variables
? Expressions (e1 e2)
? Lambda abstractions (¦Ëx. e)
Lambda Calculus (I)
? true = ¦Ëxy. x
? false = ¦Ëxy. y
? NOT a = (a)(false)(true)
? a AND b = (a)(b)(false)
? a OR b = (a)(true)(b)
? a XOR b = (a)((b)(false)(true))(b)
Haskell
? Academic origin
? Named in honor of Haskell Curry
? De?ned by a committee
? First version released on 98 (Haskell 98)
Features
? Pureness
? Type Inference
? Algebraic datatypes (ADTs)
? Pattern Matching
? Lazyness
? High Order Functions
? Curri?cation (aka Partial Application)
? Type Classes
? Monads
Pureness
? No side-effects
? A function call can have no effect other
than to compute its result
? Expressions can be evaluated at any time
? Programs are ¡°referentially transparent¡±
Good for:
* reasoning
* compiler optimization
* concurrency
Type Inference
Let¡¯s see the types for these declarations:
four = 4
add x y = x + y
emphasize x = x ++ ¡°!¡±
Algebraic datatypes
Enumeration:
data Season = Summer | Winter | Autumn | Spring
Product:
data Pair = Pair Int Int
Sum:
data Shape = Circle Float | Rect Float Float
Polymor?c & Recursive:
data Tree a = Leaf a | Node (Tree a) (Tree a)
Algebraic datatypes (I)
data Maybe a = Nothing | Just a
data Either a b = Left a | Right b
Pattern Matching
De?nition:
sum [] = 0
sum (elem:rest) = elem + sum rest
Application:
sum [1,2,3,10]
Pattern Matching (I)
area (Circle rad) = pi * rad ^ 2
area (Rect width height) = width * height
?rst (Pair value _) = value
High Order Functions
Functions which at least:
? Receive functions as parameters
? Return functions (aka curried functions)
High Order Functions (I)
map :: (a -> b) -> [a] -> [b]
map f [] = []
map f (x:xs) = f x : map f xs
Curri?cation
add :: Int -> Int -> Int
add x y = x + y
inc :: Int -> Int
inc = add 1
Lazyness
? aka ¡°call by need¡±
? Expressions can be evaluated when
necessary
? Allows the use of in?nite lists
Being pure helps here
Lazyness (I)
De?nition:
even_numbers :: [Int]
even_numbers = filter even [1..]
Application:
take 5 even_numbers
Lazyness (II)
fibs :: [Int]
fibs = 0 : 1 : zipWith (+) fibs (tail fibs)
From: http://en.wikipedia.org/wiki/Lazy_evaluation
Type Classes
? Created to solve the problem with numeric
operator overload and equality testing
? Some type classes de?ned by Haskell 98:
? Eq
? Read/Show
Type Classes (I)
class Eq a where
(==), (/=) :: a -> a -> Bool
x == y = not (x /= y)
x /= y = not (x == y) You can define what is
called a ¡°minimal
implementation¡±.
Type Classes (II)
data User = User { name :: String }
instance Eq User where
user1 == user2 = name user1 == name user2
instance Show User where
show user = name user
Automatic Derivation
data Season = Summer | Winter | Autumn | Spring
deriving (Show, Eq)
show Summer
> ¡°Summer¡±
Summer /=Winter
> True
Monads
? Adds to the type system a way to describe
actions
? The actions will happen in a certain order
Monads
? Common monads:
? IO
? State
? Reader
? Maybe
Monads
thing1 >>= x ->
func1 x >>= y ->
thing2 >>= _ ->
func2 y >>= z ->
return z
do
x <- thing1
y <- func1 x
thing2
z <- func2 y
return z
sugar no-sugar
Monads
class Monad m where
(>>=) :: m a -> (a -> m b) -> m b
(>>) :: m a -> m b -> m b
return :: a -> m a
¡°return¡± is a bad name, it
actually injects a value
into the monadic type.
Logger Monad
type Log = [String]
data Logger resultType = Logger (resultType, Log)
deriving Show
record x = Logger ((), [x])
instance Monad Logger where
return value = Logger (value, [])
prevLogger >>= nextAction =
let Logger (prevResult, prevLog) = prevLogger
Logger (newResult, newLog) = nextAction prevResult
in Logger (newResult, prevLog ++ newLog)
Testing?
? Go read about QuickCheck!
Want to learn more?
Freely available online:
http://book.realworldhaskell.org/
Your Knowledge Portfolio
"Learn at least one new language every year.
[...] Different languages solve the same
problems in different ways. By learning several
different approaches, you can help broaden
your thinking and avoid getting stuck in a
rut."
The Pragmatic Programmer
Functional
Programming
with Haskell
Adriano Bonat
abonat@thoughtworks.com
@tanob

More Related Content

What's hot (20)

Reasoning about laziness
Reasoning about lazinessReasoning about laziness
Reasoning about laziness
Johan Tibell
?
Haskell for data science
Haskell for data scienceHaskell for data science
Haskell for data science
John Cant
?
Beginning Haskell, Dive In, Its Not That Scary!
Beginning Haskell, Dive In, Its Not That Scary!Beginning Haskell, Dive In, Its Not That Scary!
Beginning Haskell, Dive In, Its Not That Scary!
priort
?
High-Performance Haskell
High-Performance HaskellHigh-Performance Haskell
High-Performance Haskell
Johan Tibell
?
Functional Programming - Past, Present and Future
Functional Programming - Past, Present and FutureFunctional Programming - Past, Present and Future
Functional Programming - Past, Present and Future
Pushkar Kulkarni
?
Functional ruby
Functional rubyFunctional ruby
Functional ruby
Kerry Buckley
?
Rewriting Java In Scala
Rewriting Java In ScalaRewriting Java In Scala
Rewriting Java In Scala
Skills Matter
?
Lec4
Lec4Lec4
Lec4
Nikhil Chilwant
?
The Functional Programming Triad of Folding, Scanning and Iteration - a first...
The Functional Programming Triad of Folding, Scanning and Iteration - a first...The Functional Programming Triad of Folding, Scanning and Iteration - a first...
The Functional Programming Triad of Folding, Scanning and Iteration - a first...
Philip Schwarz
?
Functions in advanced programming
Functions in advanced programmingFunctions in advanced programming
Functions in advanced programming
VisnuDharsini
?
Seminar fp
Seminar fpSeminar fp
Seminar fp
VeerapatBoonvanich1
?
Maps&hash tables
Maps&hash tablesMaps&hash tables
Maps&hash tables
Priyanka Rana
?
Data structures
Data structuresData structures
Data structures
Sneha Chopra
?
Priority Queue
Priority QueuePriority Queue
Priority Queue
Joyjit Choudhury
?
Arrays In Python | Python Array Operations | Edureka
Arrays In Python | Python Array Operations | EdurekaArrays In Python | Python Array Operations | Edureka
Arrays In Python | Python Array Operations | Edureka
Edureka!
?
Lec5
Lec5Lec5
Lec5
Nikhil Chilwant
?
Gdg almaty. §¶§å§ß§Ü§è§Ú§à§ß§Ñ§Ý§î§ß§à§Ö §á§â§à§Ô§â§Ñ§Þ§Þ§Ú§â§à§Ó§Ñ§ß§Ú§Ö §Ó Java 8
Gdg almaty. §¶§å§ß§Ü§è§Ú§à§ß§Ñ§Ý§î§ß§à§Ö §á§â§à§Ô§â§Ñ§Þ§Þ§Ú§â§à§Ó§Ñ§ß§Ú§Ö §Ó Java 8Gdg almaty. §¶§å§ß§Ü§è§Ú§à§ß§Ñ§Ý§î§ß§à§Ö §á§â§à§Ô§â§Ñ§Þ§Þ§Ú§â§à§Ó§Ñ§ß§Ú§Ö §Ó Java 8
Gdg almaty. §¶§å§ß§Ü§è§Ú§à§ß§Ñ§Ý§î§ß§à§Ö §á§â§à§Ô§â§Ñ§Þ§Þ§Ú§â§à§Ó§Ñ§ß§Ú§Ö §Ó Java 8
Madina Kamzina
?
Algorithms: I
Algorithms: IAlgorithms: I
Algorithms: I
Joyjit Choudhury
?
Arrays.pptx
Arrays.pptxArrays.pptx
Arrays.pptx
Shweta Bhatia
?
Collections In Scala
Collections In ScalaCollections In Scala
Collections In Scala
Knoldus Inc.
?
Reasoning about laziness
Reasoning about lazinessReasoning about laziness
Reasoning about laziness
Johan Tibell
?
Haskell for data science
Haskell for data scienceHaskell for data science
Haskell for data science
John Cant
?
Beginning Haskell, Dive In, Its Not That Scary!
Beginning Haskell, Dive In, Its Not That Scary!Beginning Haskell, Dive In, Its Not That Scary!
Beginning Haskell, Dive In, Its Not That Scary!
priort
?
High-Performance Haskell
High-Performance HaskellHigh-Performance Haskell
High-Performance Haskell
Johan Tibell
?
Functional Programming - Past, Present and Future
Functional Programming - Past, Present and FutureFunctional Programming - Past, Present and Future
Functional Programming - Past, Present and Future
Pushkar Kulkarni
?
Rewriting Java In Scala
Rewriting Java In ScalaRewriting Java In Scala
Rewriting Java In Scala
Skills Matter
?
The Functional Programming Triad of Folding, Scanning and Iteration - a first...
The Functional Programming Triad of Folding, Scanning and Iteration - a first...The Functional Programming Triad of Folding, Scanning and Iteration - a first...
The Functional Programming Triad of Folding, Scanning and Iteration - a first...
Philip Schwarz
?
Functions in advanced programming
Functions in advanced programmingFunctions in advanced programming
Functions in advanced programming
VisnuDharsini
?
Arrays In Python | Python Array Operations | Edureka
Arrays In Python | Python Array Operations | EdurekaArrays In Python | Python Array Operations | Edureka
Arrays In Python | Python Array Operations | Edureka
Edureka!
?
Gdg almaty. §¶§å§ß§Ü§è§Ú§à§ß§Ñ§Ý§î§ß§à§Ö §á§â§à§Ô§â§Ñ§Þ§Þ§Ú§â§à§Ó§Ñ§ß§Ú§Ö §Ó Java 8
Gdg almaty. §¶§å§ß§Ü§è§Ú§à§ß§Ñ§Ý§î§ß§à§Ö §á§â§à§Ô§â§Ñ§Þ§Þ§Ú§â§à§Ó§Ñ§ß§Ú§Ö §Ó Java 8Gdg almaty. §¶§å§ß§Ü§è§Ú§à§ß§Ñ§Ý§î§ß§à§Ö §á§â§à§Ô§â§Ñ§Þ§Þ§Ú§â§à§Ó§Ñ§ß§Ú§Ö §Ó Java 8
Gdg almaty. §¶§å§ß§Ü§è§Ú§à§ß§Ñ§Ý§î§ß§à§Ö §á§â§à§Ô§â§Ñ§Þ§Þ§Ú§â§à§Ó§Ñ§ß§Ú§Ö §Ó Java 8
Madina Kamzina
?

Similar to Functional Programming and Haskell - TWBR Away Day 2011 (20)

Functional Programming Past Present Future
Functional Programming Past Present FutureFunctional Programming Past Present Future
Functional Programming Past Present Future
IndicThreads
?
Introduction to Functional Programming
Introduction to Functional ProgrammingIntroduction to Functional Programming
Introduction to Functional Programming
Francesco Bruni
?
Functional Programming Patterns (BuildStuff '14)
Functional Programming Patterns (BuildStuff '14)Functional Programming Patterns (BuildStuff '14)
Functional Programming Patterns (BuildStuff '14)
Scott Wlaschin
?
OOPS Object oriented Programming PPT Tutorial
OOPS Object oriented Programming PPT TutorialOOPS Object oriented Programming PPT Tutorial
OOPS Object oriented Programming PPT Tutorial
amitnitpatna
?
An Introduction to Functional Programming using Haskell
An Introduction to Functional Programming using HaskellAn Introduction to Functional Programming using Haskell
An Introduction to Functional Programming using Haskell
Michel Rijnders
?
Functional Programming #FTW
Functional Programming #FTWFunctional Programming #FTW
Functional Programming #FTW
Adriano Bonat
?
Java/Scala Lab: §¡§ß§Ñ§ä§à§Ý§Ú§Û §¬§Þ§Ö§ä§ð§Ü - Scala SubScript: §¡§Ý§Ô§Ö§Ò§â§Ñ §Õ§Ý§ñ §â§Ö§Ñ§Ü§ä§Ú§Ó§ß§à§Ô§à §á§â...
Java/Scala Lab: §¡§ß§Ñ§ä§à§Ý§Ú§Û §¬§Þ§Ö§ä§ð§Ü - Scala SubScript: §¡§Ý§Ô§Ö§Ò§â§Ñ §Õ§Ý§ñ §â§Ö§Ñ§Ü§ä§Ú§Ó§ß§à§Ô§à §á§â...Java/Scala Lab: §¡§ß§Ñ§ä§à§Ý§Ú§Û §¬§Þ§Ö§ä§ð§Ü - Scala SubScript: §¡§Ý§Ô§Ö§Ò§â§Ñ §Õ§Ý§ñ §â§Ö§Ñ§Ü§ä§Ú§Ó§ß§à§Ô§à §á§â...
Java/Scala Lab: §¡§ß§Ñ§ä§à§Ý§Ú§Û §¬§Þ§Ö§ä§ð§Ü - Scala SubScript: §¡§Ý§Ô§Ö§Ò§â§Ñ §Õ§Ý§ñ §â§Ö§Ñ§Ü§ä§Ú§Ó§ß§à§Ô§à §á§â...
GeeksLab Odessa
?
INTRODUCTION TO LISP
INTRODUCTION TO LISPINTRODUCTION TO LISP
INTRODUCTION TO LISP
Nilt1234
?
The Fuss about || Haskell | Scala | F# ||
The Fuss about || Haskell | Scala | F# ||The Fuss about || Haskell | Scala | F# ||
The Fuss about || Haskell | Scala | F# ||
Ashwin Rao
?
L4 functions
L4 functionsL4 functions
L4 functions
mondalakash2012
?
Five Languages in a Moment
Five Languages in a MomentFive Languages in a Moment
Five Languages in a Moment
Sergio Gil
?
Monads and Monoids by Oleksiy Dyagilev
Monads and Monoids by Oleksiy DyagilevMonads and Monoids by Oleksiy Dyagilev
Monads and Monoids by Oleksiy Dyagilev
JavaDayUA
?
TI1220 Lecture 6: First-class Functions
TI1220 Lecture 6: First-class FunctionsTI1220 Lecture 6: First-class Functions
TI1220 Lecture 6: First-class Functions
Eelco Visser
?
Munihac 2018 - Beautiful Template Haskell
Munihac 2018 - Beautiful Template HaskellMunihac 2018 - Beautiful Template Haskell
Munihac 2018 - Beautiful Template Haskell
Matthew Pickering
?
10. haskell Modules
10. haskell Modules10. haskell Modules
10. haskell Modules
Sebastian Rettig
?
Monads in Swift
Monads in SwiftMonads in Swift
Monads in Swift
Vincent Pradeilles
?
Functional Scala
Functional ScalaFunctional Scala
Functional Scala
Stan Lea
?
Python Basics
Python BasicsPython Basics
Python Basics
tusharpanda88
?
Yin Yangs of Software Development
Yin Yangs of Software DevelopmentYin Yangs of Software Development
Yin Yangs of Software Development
Naveenkumar Muguda
?
How to start functional programming (in Scala): Day1
How to start functional programming (in Scala): Day1How to start functional programming (in Scala): Day1
How to start functional programming (in Scala): Day1
Taisuke Oe
?
Functional Programming Past Present Future
Functional Programming Past Present FutureFunctional Programming Past Present Future
Functional Programming Past Present Future
IndicThreads
?
Introduction to Functional Programming
Introduction to Functional ProgrammingIntroduction to Functional Programming
Introduction to Functional Programming
Francesco Bruni
?
Functional Programming Patterns (BuildStuff '14)
Functional Programming Patterns (BuildStuff '14)Functional Programming Patterns (BuildStuff '14)
Functional Programming Patterns (BuildStuff '14)
Scott Wlaschin
?
OOPS Object oriented Programming PPT Tutorial
OOPS Object oriented Programming PPT TutorialOOPS Object oriented Programming PPT Tutorial
OOPS Object oriented Programming PPT Tutorial
amitnitpatna
?
An Introduction to Functional Programming using Haskell
An Introduction to Functional Programming using HaskellAn Introduction to Functional Programming using Haskell
An Introduction to Functional Programming using Haskell
Michel Rijnders
?
Functional Programming #FTW
Functional Programming #FTWFunctional Programming #FTW
Functional Programming #FTW
Adriano Bonat
?
Java/Scala Lab: §¡§ß§Ñ§ä§à§Ý§Ú§Û §¬§Þ§Ö§ä§ð§Ü - Scala SubScript: §¡§Ý§Ô§Ö§Ò§â§Ñ §Õ§Ý§ñ §â§Ö§Ñ§Ü§ä§Ú§Ó§ß§à§Ô§à §á§â...
Java/Scala Lab: §¡§ß§Ñ§ä§à§Ý§Ú§Û §¬§Þ§Ö§ä§ð§Ü - Scala SubScript: §¡§Ý§Ô§Ö§Ò§â§Ñ §Õ§Ý§ñ §â§Ö§Ñ§Ü§ä§Ú§Ó§ß§à§Ô§à §á§â...Java/Scala Lab: §¡§ß§Ñ§ä§à§Ý§Ú§Û §¬§Þ§Ö§ä§ð§Ü - Scala SubScript: §¡§Ý§Ô§Ö§Ò§â§Ñ §Õ§Ý§ñ §â§Ö§Ñ§Ü§ä§Ú§Ó§ß§à§Ô§à §á§â...
Java/Scala Lab: §¡§ß§Ñ§ä§à§Ý§Ú§Û §¬§Þ§Ö§ä§ð§Ü - Scala SubScript: §¡§Ý§Ô§Ö§Ò§â§Ñ §Õ§Ý§ñ §â§Ö§Ñ§Ü§ä§Ú§Ó§ß§à§Ô§à §á§â...
GeeksLab Odessa
?
INTRODUCTION TO LISP
INTRODUCTION TO LISPINTRODUCTION TO LISP
INTRODUCTION TO LISP
Nilt1234
?
The Fuss about || Haskell | Scala | F# ||
The Fuss about || Haskell | Scala | F# ||The Fuss about || Haskell | Scala | F# ||
The Fuss about || Haskell | Scala | F# ||
Ashwin Rao
?
Five Languages in a Moment
Five Languages in a MomentFive Languages in a Moment
Five Languages in a Moment
Sergio Gil
?
Monads and Monoids by Oleksiy Dyagilev
Monads and Monoids by Oleksiy DyagilevMonads and Monoids by Oleksiy Dyagilev
Monads and Monoids by Oleksiy Dyagilev
JavaDayUA
?
TI1220 Lecture 6: First-class Functions
TI1220 Lecture 6: First-class FunctionsTI1220 Lecture 6: First-class Functions
TI1220 Lecture 6: First-class Functions
Eelco Visser
?
Munihac 2018 - Beautiful Template Haskell
Munihac 2018 - Beautiful Template HaskellMunihac 2018 - Beautiful Template Haskell
Munihac 2018 - Beautiful Template Haskell
Matthew Pickering
?
Functional Scala
Functional ScalaFunctional Scala
Functional Scala
Stan Lea
?
Yin Yangs of Software Development
Yin Yangs of Software DevelopmentYin Yangs of Software Development
Yin Yangs of Software Development
Naveenkumar Muguda
?
How to start functional programming (in Scala): Day1
How to start functional programming (in Scala): Day1How to start functional programming (in Scala): Day1
How to start functional programming (in Scala): Day1
Taisuke Oe
?

Recently uploaded (20)

A General introduction to Ad ranking algorithms
A General introduction to Ad ranking algorithmsA General introduction to Ad ranking algorithms
A General introduction to Ad ranking algorithms
Buhwan Jeong
?
Dev Dives: Unleash the power of macOS Automation with UiPath
Dev Dives: Unleash the power of macOS Automation with UiPathDev Dives: Unleash the power of macOS Automation with UiPath
Dev Dives: Unleash the power of macOS Automation with UiPath
UiPathCommunity
?
Getting the Best of TrueDEM ¨C April News & Updates
Getting the Best of TrueDEM ¨C April News & UpdatesGetting the Best of TrueDEM ¨C April News & Updates
Getting the Best of TrueDEM ¨C April News & Updates
panagenda
?
ºÝºÝߣs from Perth MuleSoft Meetup March 2025
ºÝºÝߣs from Perth MuleSoft Meetup March 2025ºÝºÝߣs from Perth MuleSoft Meetup March 2025
ºÝºÝߣs from Perth MuleSoft Meetup March 2025
Michael Price
?
Smarter RAG Pipelines: Scaling Search with Milvus and Feast
Smarter RAG Pipelines: Scaling Search with Milvus and FeastSmarter RAG Pipelines: Scaling Search with Milvus and Feast
Smarter RAG Pipelines: Scaling Search with Milvus and Feast
Zilliz
?
Dragino¥×¥í¥À¥¯¥È¥«¥¿¥í¥° LoRaWAN NB-IoT LTE cat.M1ÉÌÆ·¥ê¥¹¥È
Dragino¥×¥í¥À¥¯¥È¥«¥¿¥í¥° LoRaWAN  NB-IoT  LTE cat.M1ÉÌÆ·¥ê¥¹¥ÈDragino¥×¥í¥À¥¯¥È¥«¥¿¥í¥° LoRaWAN  NB-IoT  LTE cat.M1ÉÌÆ·¥ê¥¹¥È
Dragino¥×¥í¥À¥¯¥È¥«¥¿¥í¥° LoRaWAN NB-IoT LTE cat.M1ÉÌÆ·¥ê¥¹¥È
CRI Japan, Inc.
?
Java on AWS Without the Headaches - Fast Builds, Cheap Deploys, No Kubernetes
Java on AWS Without the Headaches - Fast Builds, Cheap Deploys, No KubernetesJava on AWS Without the Headaches - Fast Builds, Cheap Deploys, No Kubernetes
Java on AWS Without the Headaches - Fast Builds, Cheap Deploys, No Kubernetes
VictorSzoltysek
?
CIOs Speak Out - A Research Series by Jasper Colin
CIOs Speak Out - A Research Series by Jasper ColinCIOs Speak Out - A Research Series by Jasper Colin
CIOs Speak Out - A Research Series by Jasper Colin
Jasper Colin
?
How Air Coil Inductors Work By Cet Technology
How Air Coil Inductors Work By Cet TechnologyHow Air Coil Inductors Work By Cet Technology
How Air Coil Inductors Work By Cet Technology
CET Technology
?
Build with AI on Google Cloud Session #5
Build with AI on Google Cloud Session #5Build with AI on Google Cloud Session #5
Build with AI on Google Cloud Session #5
Margaret Maynard-Reid
?
Rene-Peinado-A-Maritime-Professionals-Journey---.pptx
Rene-Peinado-A-Maritime-Professionals-Journey---.pptxRene-Peinado-A-Maritime-Professionals-Journey---.pptx
Rene-Peinado-A-Maritime-Professionals-Journey---.pptx
Rene Peinado
?
Testing Tools for Accessibility Enhancement Part II.pptx
Testing Tools for Accessibility Enhancement Part II.pptxTesting Tools for Accessibility Enhancement Part II.pptx
Testing Tools for Accessibility Enhancement Part II.pptx
Julia Undeutsch
?
Packaging your App for AppExchange ¨C Managed Vs Unmanaged.pptx
Packaging your App for AppExchange ¨C Managed Vs Unmanaged.pptxPackaging your App for AppExchange ¨C Managed Vs Unmanaged.pptx
Packaging your App for AppExchange ¨C Managed Vs Unmanaged.pptx
mohayyudin7826
?
STARLINK-JIO-AIRTEL Security issues to Ponder
STARLINK-JIO-AIRTEL Security issues to PonderSTARLINK-JIO-AIRTEL Security issues to Ponder
STARLINK-JIO-AIRTEL Security issues to Ponder
anupriti
?
Rens van de Schoot - Mensen, machines en de zoektocht naar het laatste releva...
Rens van de Schoot - Mensen, machines en de zoektocht naar het laatste releva...Rens van de Schoot - Mensen, machines en de zoektocht naar het laatste releva...
Rens van de Schoot - Mensen, machines en de zoektocht naar het laatste releva...
voginip
?
Building High-Impact Teams Beyond the Product Triad.pdf
Building High-Impact Teams Beyond the Product Triad.pdfBuilding High-Impact Teams Beyond the Product Triad.pdf
Building High-Impact Teams Beyond the Product Triad.pdf
Rafael Burity
?
Columbia Weather Systems - Product Overview
Columbia Weather Systems - Product OverviewColumbia Weather Systems - Product Overview
Columbia Weather Systems - Product Overview
Columbia Weather Systems
?
Innovative Web Design | Malachite Technologies
Innovative Web Design | Malachite TechnologiesInnovative Web Design | Malachite Technologies
Innovative Web Design | Malachite Technologies
malachitetechnologie1
?
The Road to SAP S4HANA Cloud with SAP Activate.pptx
The Road to SAP S4HANA Cloud with SAP Activate.pptxThe Road to SAP S4HANA Cloud with SAP Activate.pptx
The Road to SAP S4HANA Cloud with SAP Activate.pptx
zsbaranyai
?
Generative AI & Telco track at AMLD 2024
Generative AI & Telco track at AMLD 2024Generative AI & Telco track at AMLD 2024
Generative AI & Telco track at AMLD 2024
Laurent Ciavaglia
?
A General introduction to Ad ranking algorithms
A General introduction to Ad ranking algorithmsA General introduction to Ad ranking algorithms
A General introduction to Ad ranking algorithms
Buhwan Jeong
?
Dev Dives: Unleash the power of macOS Automation with UiPath
Dev Dives: Unleash the power of macOS Automation with UiPathDev Dives: Unleash the power of macOS Automation with UiPath
Dev Dives: Unleash the power of macOS Automation with UiPath
UiPathCommunity
?
Getting the Best of TrueDEM ¨C April News & Updates
Getting the Best of TrueDEM ¨C April News & UpdatesGetting the Best of TrueDEM ¨C April News & Updates
Getting the Best of TrueDEM ¨C April News & Updates
panagenda
?
ºÝºÝߣs from Perth MuleSoft Meetup March 2025
ºÝºÝߣs from Perth MuleSoft Meetup March 2025ºÝºÝߣs from Perth MuleSoft Meetup March 2025
ºÝºÝߣs from Perth MuleSoft Meetup March 2025
Michael Price
?
Smarter RAG Pipelines: Scaling Search with Milvus and Feast
Smarter RAG Pipelines: Scaling Search with Milvus and FeastSmarter RAG Pipelines: Scaling Search with Milvus and Feast
Smarter RAG Pipelines: Scaling Search with Milvus and Feast
Zilliz
?
Dragino¥×¥í¥À¥¯¥È¥«¥¿¥í¥° LoRaWAN NB-IoT LTE cat.M1ÉÌÆ·¥ê¥¹¥È
Dragino¥×¥í¥À¥¯¥È¥«¥¿¥í¥° LoRaWAN  NB-IoT  LTE cat.M1ÉÌÆ·¥ê¥¹¥ÈDragino¥×¥í¥À¥¯¥È¥«¥¿¥í¥° LoRaWAN  NB-IoT  LTE cat.M1ÉÌÆ·¥ê¥¹¥È
Dragino¥×¥í¥À¥¯¥È¥«¥¿¥í¥° LoRaWAN NB-IoT LTE cat.M1ÉÌÆ·¥ê¥¹¥È
CRI Japan, Inc.
?
Java on AWS Without the Headaches - Fast Builds, Cheap Deploys, No Kubernetes
Java on AWS Without the Headaches - Fast Builds, Cheap Deploys, No KubernetesJava on AWS Without the Headaches - Fast Builds, Cheap Deploys, No Kubernetes
Java on AWS Without the Headaches - Fast Builds, Cheap Deploys, No Kubernetes
VictorSzoltysek
?
CIOs Speak Out - A Research Series by Jasper Colin
CIOs Speak Out - A Research Series by Jasper ColinCIOs Speak Out - A Research Series by Jasper Colin
CIOs Speak Out - A Research Series by Jasper Colin
Jasper Colin
?
How Air Coil Inductors Work By Cet Technology
How Air Coil Inductors Work By Cet TechnologyHow Air Coil Inductors Work By Cet Technology
How Air Coil Inductors Work By Cet Technology
CET Technology
?
Build with AI on Google Cloud Session #5
Build with AI on Google Cloud Session #5Build with AI on Google Cloud Session #5
Build with AI on Google Cloud Session #5
Margaret Maynard-Reid
?
Rene-Peinado-A-Maritime-Professionals-Journey---.pptx
Rene-Peinado-A-Maritime-Professionals-Journey---.pptxRene-Peinado-A-Maritime-Professionals-Journey---.pptx
Rene-Peinado-A-Maritime-Professionals-Journey---.pptx
Rene Peinado
?
Testing Tools for Accessibility Enhancement Part II.pptx
Testing Tools for Accessibility Enhancement Part II.pptxTesting Tools for Accessibility Enhancement Part II.pptx
Testing Tools for Accessibility Enhancement Part II.pptx
Julia Undeutsch
?
Packaging your App for AppExchange ¨C Managed Vs Unmanaged.pptx
Packaging your App for AppExchange ¨C Managed Vs Unmanaged.pptxPackaging your App for AppExchange ¨C Managed Vs Unmanaged.pptx
Packaging your App for AppExchange ¨C Managed Vs Unmanaged.pptx
mohayyudin7826
?
STARLINK-JIO-AIRTEL Security issues to Ponder
STARLINK-JIO-AIRTEL Security issues to PonderSTARLINK-JIO-AIRTEL Security issues to Ponder
STARLINK-JIO-AIRTEL Security issues to Ponder
anupriti
?
Rens van de Schoot - Mensen, machines en de zoektocht naar het laatste releva...
Rens van de Schoot - Mensen, machines en de zoektocht naar het laatste releva...Rens van de Schoot - Mensen, machines en de zoektocht naar het laatste releva...
Rens van de Schoot - Mensen, machines en de zoektocht naar het laatste releva...
voginip
?
Building High-Impact Teams Beyond the Product Triad.pdf
Building High-Impact Teams Beyond the Product Triad.pdfBuilding High-Impact Teams Beyond the Product Triad.pdf
Building High-Impact Teams Beyond the Product Triad.pdf
Rafael Burity
?
Innovative Web Design | Malachite Technologies
Innovative Web Design | Malachite TechnologiesInnovative Web Design | Malachite Technologies
Innovative Web Design | Malachite Technologies
malachitetechnologie1
?
The Road to SAP S4HANA Cloud with SAP Activate.pptx
The Road to SAP S4HANA Cloud with SAP Activate.pptxThe Road to SAP S4HANA Cloud with SAP Activate.pptx
The Road to SAP S4HANA Cloud with SAP Activate.pptx
zsbaranyai
?
Generative AI & Telco track at AMLD 2024
Generative AI & Telco track at AMLD 2024Generative AI & Telco track at AMLD 2024
Generative AI & Telco track at AMLD 2024
Laurent Ciavaglia
?

Functional Programming and Haskell - TWBR Away Day 2011

  • 2. Why FP? ? Source of new ideas ? Expressiveness ? Multi-core CPUs ? Different paradigm New ideas: Garbage collection (LISP) Type inference (simply typed lambda calculus) Generics Type classes Expressiveness: DSLs
  • 3. What is it? ? Different programming paradigm ? OO ? Logic ? Procedural ? Functions are the main element in the language
  • 4. Function applications ¡°Functional programming is so called because a program consists entirely of functions. [...] Typically the main function is de?ned in terms of other functions, which in turn are de?ned in terms of still more functions, until at the bottom level the functions are language primitives.¡± John Hughes, 1989 -Why functional programming matters
  • 5. Origin Alonzo Church developed Lambda Calculus as part of his investigations on Math foundations on 1936.
  • 6. Lambda Calculus ? Variables ? Expressions (e1 e2) ? Lambda abstractions (¦Ëx. e)
  • 7. Lambda Calculus (I) ? true = ¦Ëxy. x ? false = ¦Ëxy. y ? NOT a = (a)(false)(true) ? a AND b = (a)(b)(false) ? a OR b = (a)(true)(b) ? a XOR b = (a)((b)(false)(true))(b)
  • 8. Haskell ? Academic origin ? Named in honor of Haskell Curry ? De?ned by a committee ? First version released on 98 (Haskell 98)
  • 9. Features ? Pureness ? Type Inference ? Algebraic datatypes (ADTs) ? Pattern Matching ? Lazyness ? High Order Functions ? Curri?cation (aka Partial Application) ? Type Classes ? Monads
  • 10. Pureness ? No side-effects ? A function call can have no effect other than to compute its result ? Expressions can be evaluated at any time ? Programs are ¡°referentially transparent¡± Good for: * reasoning * compiler optimization * concurrency
  • 11. Type Inference Let¡¯s see the types for these declarations: four = 4 add x y = x + y emphasize x = x ++ ¡°!¡±
  • 12. Algebraic datatypes Enumeration: data Season = Summer | Winter | Autumn | Spring Product: data Pair = Pair Int Int Sum: data Shape = Circle Float | Rect Float Float Polymor?c & Recursive: data Tree a = Leaf a | Node (Tree a) (Tree a)
  • 13. Algebraic datatypes (I) data Maybe a = Nothing | Just a data Either a b = Left a | Right b
  • 14. Pattern Matching De?nition: sum [] = 0 sum (elem:rest) = elem + sum rest Application: sum [1,2,3,10]
  • 15. Pattern Matching (I) area (Circle rad) = pi * rad ^ 2 area (Rect width height) = width * height ?rst (Pair value _) = value
  • 16. High Order Functions Functions which at least: ? Receive functions as parameters ? Return functions (aka curried functions)
  • 17. High Order Functions (I) map :: (a -> b) -> [a] -> [b] map f [] = [] map f (x:xs) = f x : map f xs
  • 18. Curri?cation add :: Int -> Int -> Int add x y = x + y inc :: Int -> Int inc = add 1
  • 19. Lazyness ? aka ¡°call by need¡± ? Expressions can be evaluated when necessary ? Allows the use of in?nite lists Being pure helps here
  • 20. Lazyness (I) De?nition: even_numbers :: [Int] even_numbers = filter even [1..] Application: take 5 even_numbers
  • 21. Lazyness (II) fibs :: [Int] fibs = 0 : 1 : zipWith (+) fibs (tail fibs) From: http://en.wikipedia.org/wiki/Lazy_evaluation
  • 22. Type Classes ? Created to solve the problem with numeric operator overload and equality testing ? Some type classes de?ned by Haskell 98: ? Eq ? Read/Show
  • 23. Type Classes (I) class Eq a where (==), (/=) :: a -> a -> Bool x == y = not (x /= y) x /= y = not (x == y) You can define what is called a ¡°minimal implementation¡±.
  • 24. Type Classes (II) data User = User { name :: String } instance Eq User where user1 == user2 = name user1 == name user2 instance Show User where show user = name user
  • 25. Automatic Derivation data Season = Summer | Winter | Autumn | Spring deriving (Show, Eq) show Summer > ¡°Summer¡± Summer /=Winter > True
  • 26. Monads ? Adds to the type system a way to describe actions ? The actions will happen in a certain order
  • 27. Monads ? Common monads: ? IO ? State ? Reader ? Maybe
  • 28. Monads thing1 >>= x -> func1 x >>= y -> thing2 >>= _ -> func2 y >>= z -> return z do x <- thing1 y <- func1 x thing2 z <- func2 y return z sugar no-sugar
  • 29. Monads class Monad m where (>>=) :: m a -> (a -> m b) -> m b (>>) :: m a -> m b -> m b return :: a -> m a ¡°return¡± is a bad name, it actually injects a value into the monadic type.
  • 30. Logger Monad type Log = [String] data Logger resultType = Logger (resultType, Log) deriving Show record x = Logger ((), [x]) instance Monad Logger where return value = Logger (value, []) prevLogger >>= nextAction = let Logger (prevResult, prevLog) = prevLogger Logger (newResult, newLog) = nextAction prevResult in Logger (newResult, prevLog ++ newLog)
  • 31. Testing? ? Go read about QuickCheck!
  • 32. Want to learn more? Freely available online: http://book.realworldhaskell.org/
  • 33. Your Knowledge Portfolio "Learn at least one new language every year. [...] Different languages solve the same problems in different ways. By learning several different approaches, you can help broaden your thinking and avoid getting stuck in a rut." The Pragmatic Programmer