SlideShare a Scribd company logo
Introduction
to Haskell
Luca Molteni
Haskell ITA Meeting 17/10/2015
Topics
Basic Concepts
Currying
Functors
• Functions are first-class, that is, functions are
values which can be used in exactly the same
ways as any other sort of value.
• The meaning of Haskell programs is focused
around evaluating expressions rather than
executing instructions.
3 * (9 + 5)
=> 3 * 14
=> 42
3 * (9 + 5)
=> 3 * 9 + 3 * 5
=> 27 + 3 * 5
=> 27 + 15
=> 42
simple x y z = x * (y + z)
simple 3 9 5
=> 3 * (9 + 5)
=> 3 * 14
=> 42
“An expression is said to be referentially
transparent if it can be replaced with its value
without changing the behavior of a program (in
other words, yielding a program that has the
same effects and output on the same input).”
Referential transparency
simple a b c = simple a c b
simple a b c
=> { unfold }
a * (b + c)
=> { commutativity }
a * (c + b)
=> { fold }
simple a c b
x = x + 1
x = x + 1
x
=> x + 1
=> (x + 1) + 1
=> ((x + 1) + 1) +1
=> (((x + 1) +1) +1) +1
...
“Because a referentially transparent expression
can be evaluated at any time, it is not
necessary to define sequence points nor any
guarantee of the order of evaluation at all.
Programming done without these
considerations is called
purely functional programming.”
Referential transparency
totalArea r1 r2 r3
= pi * r1 ^ 2 +
pi * r2 ^ 2 +
pi * r3 ^ 2
Sum of the areas of three circles
with radii r1, r2, r3
circleArea r = pi * r ^ 2
totalArea r1 r2 r3
= circleArea r1 +
circleArea r2 +
circleArea r3
Sum of the areas of three circles
with radii r1, r2, r3
r1 = 3
r2 = 4
r3 = 5
radii = r1 : r2 : r3 : []
Sum of the areas of n circles?
radii :: [Float]
radii = r1 : r2 : r3 : []
Sum of the areas of n circles?
List
data List a = []
| a : List a
totalArea :: [Float] -> Float
totalArea [] = 0
Sum of the areas of three circles
with radii r1, r2, r3
totalArea :: [Float] -> Float
totalArea [] = 0
totalArea (x : xs) =
circleArea x + totalArea xs
Sum of the areas of three circles
with radii r1, r2, r3
square :: [Int] -> [Int]
square [] = []
square (x : xs) = (x ^ 2) : square xs
Square of list
Map
map :: (a -> b) -> [a] -> [b]
totalArea :: [Float] -> [Float]
totalArea = map circleArea
Sum of the areas of three circles
with radii r1, r2, r3
totalArea :: [Float] -> Float
totalArea = sum . map circleArea
Sum of the areas of three circles
with radii r1, r2, r3
Currying
simple :: (Int -> Int -> Int) -> Int
simple (x y z)
Currying
simple :: Int -> Int -> Int -> Int
simple x y z = x * ( y + z)
(((simple x) y) z)
Currying
simple 5
Partial Application
simple 5 :: Int -> Int -> Int
simple 5
Partial Application
simple :: Int -> Int -> Int -> Int
simple 5 :: Int -> Int -> Int
simple 5 2 :: Int -> Int
simple 5 2 3 :: Int
Partial Application
Point free programming
Tacit programming (point-free programming) is a
programming paradigm in which a function definition
does not include information regarding its arguments,
using combinators and function composition [...]
instead of variables.
Point free programming
map (x -> increment x) [2,3,4]
[3,4,5]
map increment [2,3,4]
[3,4,5]
map (x -> x + 1) [2,3,4]
[3,4,5]
map (+1) [2,3,4]
[3,4,5]
increment :: Int -> Int
increment x = x + 1
Point free programming
mf criteria operator list = filter criteria (map operator list)
mf = (. map) . (.) . filter
Functors
Functors
map :: (a -> b) -> [a] -> [b]
data List a = []
| a : List a
Functors
treeMap :: (a -> b) -> Tree a -> Tree b
data Tree a = Leaf a
| Branch (Tree a) (Tree a)
Functors
treeMap :: (a -> b) -> Tree a -> Tree b
map :: (a -> b) -> [a] -> [b]
Functors
thingMap :: (a -> b) -> f a -> f b
Functors
A typeclass is a class of types that behave
similarly.
Functors
class Functor f where
fmap :: (a -> b) -> f a -> f b
Functors
instance Functor [] where
fmap = map
Functors / Maybe
data Maybe a = Just a | Nothing
Functors
instance Functor Maybe where
fmap _ Nothing = Nothing
fmap f (Just a) = Just (f a)
Functors / Maybe
fmap (* 2) (Just 2)
Just 4
fmap (+ 5) (Just 2)
Just 7
fmap (+ 5) (Nothing)
Nothing
The End
@volothamp
It’s not about the destination.
It’s about the journey.

More Related Content

What's hot

Bubble sort
Bubble sortBubble sort
Bubble sort
Manek Ar
 
Data structure
Data structureData structure
Data structure
Muhammad Farhan
 
Recursion
RecursionRecursion
Recursion
Abdur Rehman
 
Data structures chapter 1
Data structures chapter  1Data structures chapter  1
Data structures chapter 1
priyavanimurugarajan
 
Randomizing quicksort algorith with example
Randomizing quicksort algorith with exampleRandomizing quicksort algorith with example
Randomizing quicksort algorith with example
maamir farooq
 
properties, application and issues of support vector machine
properties, application and issues of support vector machineproperties, application and issues of support vector machine
properties, application and issues of support vector machine
Dr. Radhey Shyam
 
Unit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTINGUnit 6 dsa SEARCHING AND SORTING
linear search and binary search
linear search and binary searchlinear search and binary search
linear search and binary search
Zia Ush Shamszaman
 
Sorting
SortingSorting
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notationsNikhil Sharma
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
Abdelrahman Saleh
 
Computer architecture
Computer architectureComputer architecture
Computer architectureSanjeev Patel
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithmsmultimedia9
 
List,tuple,dictionary
List,tuple,dictionaryList,tuple,dictionary
List,tuple,dictionary
nitamhaske
 
Unix notes
Unix notesUnix notes
Sum of subset problem.pptx
Sum of subset problem.pptxSum of subset problem.pptx
Sum of subset problem.pptx
V.V.Vanniaperumal College for Women
 
Selection sort
Selection sortSelection sort
Selection sort
amna izzat
 
Dictionary
DictionaryDictionary
Dictionary
Pooja B S
 

What's hot (20)

Bubble sort
Bubble sortBubble sort
Bubble sort
 
Data structure
Data structureData structure
Data structure
 
Recursion
RecursionRecursion
Recursion
 
Data structures chapter 1
Data structures chapter  1Data structures chapter  1
Data structures chapter 1
 
Randomizing quicksort algorith with example
Randomizing quicksort algorith with exampleRandomizing quicksort algorith with example
Randomizing quicksort algorith with example
 
Merge sort
Merge sortMerge sort
Merge sort
 
properties, application and issues of support vector machine
properties, application and issues of support vector machineproperties, application and issues of support vector machine
properties, application and issues of support vector machine
 
Unit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTINGUnit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTING
 
linear search and binary search
linear search and binary searchlinear search and binary search
linear search and binary search
 
Sorting
SortingSorting
Sorting
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
 
Recursion
RecursionRecursion
Recursion
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Computer architecture
Computer architectureComputer architecture
Computer architecture
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
List,tuple,dictionary
List,tuple,dictionaryList,tuple,dictionary
List,tuple,dictionary
 
Unix notes
Unix notesUnix notes
Unix notes
 
Sum of subset problem.pptx
Sum of subset problem.pptxSum of subset problem.pptx
Sum of subset problem.pptx
 
Selection sort
Selection sortSelection sort
Selection sort
 
Dictionary
DictionaryDictionary
Dictionary
 

Similar to Introduction to haskell

Functions in advanced programming
Functions in advanced programmingFunctions in advanced programming
Functions in advanced programming
VisnuDharsini
 
Map, Reduce and Filter in Swift
Map, Reduce and Filter in SwiftMap, Reduce and Filter in Swift
Map, Reduce and Filter in Swift
Aleksandras Smirnovas
 
Why Haskell Matters
Why Haskell MattersWhy Haskell Matters
Why Haskell Matters
romanandreg
 
R tutorial for a windows environment
R tutorial for a windows environmentR tutorial for a windows environment
R tutorial for a windows environment
Yogendra Chaubey
 
R Programming Intro
R Programming IntroR Programming Intro
R Programming Intro
062MayankSinghal
 
Programming in R
Programming in RProgramming in R
Programming in R
Smruti Sarangi
 
Scala by Luc Duponcheel
Scala by Luc DuponcheelScala by Luc Duponcheel
Scala by Luc Duponcheel
Stephan Janssen
 
R basics
R basicsR basics
R basics
Sagun Baijal
 
Monads in Swift
Monads in SwiftMonads in Swift
Monads in Swift
Vincent Pradeilles
 
Python High Level Functions_Ch 11.ppt
Python High Level Functions_Ch 11.pptPython High Level Functions_Ch 11.ppt
Python High Level Functions_Ch 11.ppt
AnishaJ7
 
On fuctional programming, high order functions, ML
On fuctional programming, high order functions, MLOn fuctional programming, high order functions, ML
On fuctional programming, high order functions, ML
Simone Di Maulo
 
Basic_analysis.ppt
Basic_analysis.pptBasic_analysis.ppt
Basic_analysis.ppt
SoumyaJ3
 
Matlab ppt
Matlab pptMatlab ppt
Matlab ppt
chestialtaff
 
Loops and functions in r
Loops and functions in rLoops and functions in r
Loops and functions in r
manikanta361
 
Functional Programming by Examples using Haskell
Functional Programming by Examples using HaskellFunctional Programming by Examples using Haskell
Functional Programming by Examples using Haskell
goncharenko
 
chapter1.ppt
chapter1.pptchapter1.ppt
chapter1.ppt
SankarTerli
 
TI1220 Lecture 6: First-class Functions
TI1220 Lecture 6: First-class FunctionsTI1220 Lecture 6: First-class Functions
TI1220 Lecture 6: First-class FunctionsEelco Visser
 

Similar to Introduction to haskell (20)

Functions in advanced programming
Functions in advanced programmingFunctions in advanced programming
Functions in advanced programming
 
Map, Reduce and Filter in Swift
Map, Reduce and Filter in SwiftMap, Reduce and Filter in Swift
Map, Reduce and Filter in Swift
 
Why Haskell Matters
Why Haskell MattersWhy Haskell Matters
Why Haskell Matters
 
R Language Introduction
R Language IntroductionR Language Introduction
R Language Introduction
 
R tutorial for a windows environment
R tutorial for a windows environmentR tutorial for a windows environment
R tutorial for a windows environment
 
R Programming Intro
R Programming IntroR Programming Intro
R Programming Intro
 
Programming in R
Programming in RProgramming in R
Programming in R
 
Scala by Luc Duponcheel
Scala by Luc DuponcheelScala by Luc Duponcheel
Scala by Luc Duponcheel
 
R basics
R basicsR basics
R basics
 
Monads in Swift
Monads in SwiftMonads in Swift
Monads in Swift
 
Python High Level Functions_Ch 11.ppt
Python High Level Functions_Ch 11.pptPython High Level Functions_Ch 11.ppt
Python High Level Functions_Ch 11.ppt
 
On fuctional programming, high order functions, ML
On fuctional programming, high order functions, MLOn fuctional programming, high order functions, ML
On fuctional programming, high order functions, ML
 
Learn Matlab
Learn MatlabLearn Matlab
Learn Matlab
 
Basic_analysis.ppt
Basic_analysis.pptBasic_analysis.ppt
Basic_analysis.ppt
 
Matlab ppt
Matlab pptMatlab ppt
Matlab ppt
 
Loops and functions in r
Loops and functions in rLoops and functions in r
Loops and functions in r
 
Functional Programming by Examples using Haskell
Functional Programming by Examples using HaskellFunctional Programming by Examples using Haskell
Functional Programming by Examples using Haskell
 
chapter1.ppt
chapter1.pptchapter1.ppt
chapter1.ppt
 
chapter1.ppt
chapter1.pptchapter1.ppt
chapter1.ppt
 
TI1220 Lecture 6: First-class Functions
TI1220 Lecture 6: First-class FunctionsTI1220 Lecture 6: First-class Functions
TI1220 Lecture 6: First-class Functions
 

Recently uploaded

Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Globus
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Natan Silnitsky
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
Globus
 
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBrokerSOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar
 
Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024
Paco van Beckhoven
 
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
Google
 
Corporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMSCorporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMS
Tendenci - The Open Source AMS (Association Management Software)
 
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke
 
A Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdfA Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdf
kalichargn70th171
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
Philip Schwarz
 
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxTop Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
rickgrimesss22
 
Graphic Design Crash Course for beginners
Graphic Design Crash Course for beginnersGraphic Design Crash Course for beginners
Graphic Design Crash Course for beginners
e20449
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus
 
How Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptxHow Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptx
wottaspaceseo
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Globus
 
top nidhi software solution freedownload
top nidhi software solution freedownloadtop nidhi software solution freedownload
top nidhi software solution freedownload
vrstrong314
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
XfilesPro
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
Matt Welsh
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with Platformless
WSO2
 
May Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdfMay Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdf
Adele Miller
 

Recently uploaded (20)

Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
 
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBrokerSOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBroker
 
Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024
 
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
 
Corporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMSCorporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMS
 
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume Montevideo
 
A Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdfA Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdf
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
 
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxTop Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
 
Graphic Design Crash Course for beginners
Graphic Design Crash Course for beginnersGraphic Design Crash Course for beginners
Graphic Design Crash Course for beginners
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
 
How Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptxHow Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptx
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
 
top nidhi software solution freedownload
top nidhi software solution freedownloadtop nidhi software solution freedownload
top nidhi software solution freedownload
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with Platformless
 
May Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdfMay Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdf
 

Introduction to haskell