SlideShare a Scribd company logo
Haskell for Data Science
John Cant
Haskell at a glance
● Purely functional programming language
● Compiled
● Statically typed
● Strongly typed
● Non-strict
Who on earth would use Haskell for data science?
Finance industry
Facebook
Safety critical systems
Bioinformatics
Startups
Various
Whirlwind tour of Haskell
main = putStrLn "Hello world!"
Function application
main = putStrLn "Hello world!"
Function First argument
Type signatures
(+) :: Num a => a -> a -> a
Type constraint Arguments Return type
Partial function application
(+) :: Num a => a -> a -> a
(+5) :: Num a => a -> a
Higher order functions
map :: (a -> b) -> [a] -> [b]
GHCI> map (*2) [0, 1, 2]
[0,2,4]
Data definitions
data ChessGame = NotStarted
| PlayerTurn Double Player BoardState
| CheckMate Player BoardState
Record syntax
data PersonRecord s = PersonRecord { firstName :: s
, lastName :: s
, personID :: Integer }
Pattern matching
airportSecurity ( PersonRecord "John" n _) = "Water bottles detected, Mr. " ++ n
airportSecurity _ = "Please proceed to the departure lounge "
Pattern matching
data [] a = [] | a : [a]
map _ [] = []
map f (x:xs) = f x : map f xs
data JSON = Object [(String, JSON)]
| Array [JSON]
| String String
| Number Double
| Bool Bool
| Null
Typeclasses
class ToJSON a where
toJSON :: a -> JSON
instance ToJSON (PersonRecord String) where
toJSON (PersonRecord n0 n1 i) = ...
Evaluation of expressions
foo a b = (a+b)*(a+b)
foo 3 5
*
+ +
3 5
Evaluation of expressions
*
+ +
a b
*
+
a b
Evaluation of expressions
+
3 5
*
8
*
64
Evaluation of expressions
bar = PersonRecord “bla” “bla” (bar 3 5)
bar
3 5
PersonRecord
“bla” “bla”
Evaluation of expressions
bar
3 5
PersonRecord
“bla” “bla”
STOP!!!!!
Success!
Weak Head
Normal Form
Pure functions
● Output determined only by inputs
● No side effects
=> Result independent of evaluation strategy
Impure functions
● Randomness
● File IO
● Network
● Call impure functions
● Mutations
● Hard to reason about
● Requires reasoning
Monads
Ordinary value
cube :: (Floating a) => a →
a
cube x = x * x * x
Just use the value Monad
cubeM :: (Monad m, Floating a) => m a → m a
cubeM mx = mx >>= (x → return x * x * x)
Just use the value (inside a function you’ve
bound to the monad using >>=)
Various Monad >>= implementations
IO monad
After the IO is performed
Maybe monad
If the value is not Nothing
Reader, Writer, State monad
Immediately
List monad
For each element
cubeM :: (Monad m, Floating a) => m a → m a
cubeM mx = mx >>= (x → return x * x * x)
class Monad m where
(>>=) :: m a -> (a -> m b) -> m b
return :: a -> m a
-- ...
Monads
● (In general) No way to extract value
● Result of >>= is m b, so no escape from m!
● Monads can function as tags in your source code
class Monad m where
(>>=) :: m a -> (a -> m b) -> m b
return :: a -> m a
-- ...
Monads
● Return representation of side effects
● Control evaluation order
● Move non-determinism away from pure code
● Tag values resulting from impure computation
● Store information between computations
Syntactic sugar: Imperative syntax
● Each line evaluated inside a
function passed to >>=
● Evaluation order of lines
guaranteed
● answer is the name bound to
an argument of one of these
functions. It is available to
functions defined inside this
function.
Tooling
GHC
GHCI
Cabal
Hoogle
Hackage
Haddock
Fay, Haste, GHCJS
Libraries required for data science
Fast Vectors, Arrays, Linear Algebra
Machine learning, Deep learning
Probability and statistics
Big data
Plotting, Graphs, Visualization
Vectors, Arrays, Linear Algebra
Vec, Linear, Repa, Accelerate
Use type level literals to encode dimensions of arrays (Repa, Accelerate)
Use type level literals to encode length of vectors (Linear, Vec)
Accelerate EDSL for running computations on the GPU!
Compatability - Use data types from Linear on Accelerate backends
Machine learning
LambdaNet, bpann, hfann, hnn, HaskellNN,
instinct, mines, simple-neural-networks
HMM, hmm, learning-hmm, markov-processes
svm, svm-simple
hopfield, deeplearning-hs, dnngraph
HLearn
genetics, GA, genprog, hgalib, moo, HSGEP,
simple-genetic-algorithm, SimpleEA
dtw, DynamicTimeWarp
KMeans, clustering,
heiraclus, Kmeans-vector,
gsc-weighting, hps-
kmeans, hsgsom,
hierarchical-clustering
estimator, Kalman
Probability and statistics
~40 packages
Big Data
No spark library unfortunately
Hadron
Misc Hadoop libraries
Haskell-HBase, ElasticSearch, Cassandra, MongoDB, Redis
CloudHaskell
Kafka, ZeroMQ
Various DB connectors
Plotting, graphing, visualisation
Many plotting libraries
OpenGL implementation
Elegant DSLs for writing HTML and CSS
Graphics.Rendering.Chart.Easy
Wide range of different plots (credit: timbod7)
Example: Density of OpenStreetMap points
Raw OSM points. 78Gb uncompressed. 2.9 Billion points.
Plot the density of these on a globe.
Use Triangular binning because it might look cool
Data types
Point data type. Just use the Vec library
Triangle data type. Tuple of points
A point that stores extra info, for insertion into a KD Tree
Data.Trees.KdTree
Lets use our own point implementation!
Spherical mesh generation
Spherical Mesh generation
Bin 3d GPS points onto a triangle in the mesh
B reaking
H askell
undefined
unsafePerformIO
IORef
MVar
unsafeCoerce
trace
Use these very carefully!
Haskell Problems
Jargon, complicated types
Learning curve
Haskell Problems
Preventable and unpreventable bugs
Runtime errors
Non exhaustive pattern match
Prevent with -Wall
Crashes at runtime!
Haskell Problems
Exceptions
Error/Exception handling
error
fail
Error and Exception catching monads and transformers. ErrorT
Maybe
Either
1/0
1/0
Haskell Problems
Space leaks
Very easy to accidentally exhaust system memory
Many types of space leak
http://blog.ezyang.com/2011/05/space-leak-zoo/
Enough to need their own zoo!
Memory leak
Strong reference leak
Thunk leak
Live variable leak
Streaming leak
Stack Overflow
Selector Leak
Optimization induced Leak
Thread leak
Tail recursion
length
+
1
length
+
1
length
length
len’
0 len’
Thunk
buildup
Evaluation
(1+(1+(...)))
0
1
len’
2
Intermediate values not
needed!
Thunk leak
Optimisation
possible!
Performance
This slide intentionally left blank
C/C++ > Haskell > Scripting languages
Conduits
Naive Conduit Summation
Data.IntMap
Strict or Lazy variety? Persistent or Ephemeral?
“The implementation is based on big-endian patricia trees. This data structure performs especially well on binary operations like union and
intersection. However, my benchmarks show that it is also (much) faster on insertions and deletions when compared to a generic size-balanced map
implementation (see Data.Map).
● Chris Okasaki and Andy Gill, "Fast Mergeable Integer Maps", Workshop on ML, September 1998, pages 77-86, http://citeseer.ist.psu.
edu/okasaki98fast.html
○ D.R. Morrison, "/PATRICIA -- Practical Algorithm To Retrieve Information Coded In Alphanumeric/", Journal of the ACM,
15(4), October 1968, pages 514-534.
“”
Data.IntMap is a persistent data structure!
Result => Horrendous space leak!
Fix by periodically rebuilding it.
Or, give in and use a mutable vector.
Accelerate (credit: Trevor L.
McDonell)
Thankyou!
Q+A

More Related Content

What's hot

Open Computer Vision Based Image Processing
Open Computer Vision Based Image ProcessingOpen Computer Vision Based Image Processing
Open Computer Vision Based Image Processing
NEEVEE Technologies
 
Anlysis and design of algorithms part 1
Anlysis and design of algorithms part 1Anlysis and design of algorithms part 1
Anlysis and design of algorithms part 1
Deepak John
 
Thresholding.ppt
Thresholding.pptThresholding.ppt
Thresholding.ppt
shankar64
 
Image Segmentation using Otsu's Method - Computer Graphics (UCS505) Project PPT
Image Segmentation using Otsu's Method - Computer Graphics (UCS505) Project PPTImage Segmentation using Otsu's Method - Computer Graphics (UCS505) Project PPT
Image Segmentation using Otsu's Method - Computer Graphics (UCS505) Project PPT
Akshit Arora
 
Unit 3 daa
Unit 3 daaUnit 3 daa
Unit 3 daa
Nv Thejaswini
 
Greedy algorithm
Greedy algorithmGreedy algorithm
Histogram Equalization
Histogram EqualizationHistogram Equalization
Histogram Equalization
Kalyan Acharjya
 
Graphics a buffer
Graphics a bufferGraphics a buffer
Graphics a buffer
ajeela mushtaq
 
Erosion and dilation
Erosion and dilationErosion and dilation
Erosion and dilation
Akhil .B
 
Minimum spanning tree
Minimum spanning treeMinimum spanning tree
Minimum spanning tree
Hinal Lunagariya
 
Deep Learning with PyTorch
Deep Learning with PyTorchDeep Learning with PyTorch
Deep Learning with PyTorch
Mayur Bhangale
 
10-SLR parser practice problems-02-06-2023.pptx
10-SLR parser practice problems-02-06-2023.pptx10-SLR parser practice problems-02-06-2023.pptx
10-SLR parser practice problems-02-06-2023.pptx
venkatapranaykumarGa
 
Morphological Image Processing
Morphological Image ProcessingMorphological Image Processing
Morphological Image Processing
kumari36
 
Uninformed search
Uninformed searchUninformed search
Uninformed search
Megha Sharma
 
ImageProcessing10-Segmentation(Thresholding) (1).ppt
ImageProcessing10-Segmentation(Thresholding) (1).pptImageProcessing10-Segmentation(Thresholding) (1).ppt
ImageProcessing10-Segmentation(Thresholding) (1).ppt
VikramBarapatre2
 
adaboost
adaboostadaboost
adaboost
kalung0313
 
Graph Algorithms: Breadth-First Search (BFS)
Graph Algorithms: Breadth-First Search (BFS)Graph Algorithms: Breadth-First Search (BFS)
Graph Algorithms: Breadth-First Search (BFS)
Md. Shafiuzzaman Hira
 
PRIM'S ALGORITHM
PRIM'S ALGORITHMPRIM'S ALGORITHM
PRIM'S ALGORITHM
garishma bhatia
 
Lec7: Medical Image Segmentation (I) (Radiology Applications of Segmentation,...
Lec7: Medical Image Segmentation (I) (Radiology Applications of Segmentation,...Lec7: Medical Image Segmentation (I) (Radiology Applications of Segmentation,...
Lec7: Medical Image Segmentation (I) (Radiology Applications of Segmentation,...
Ulaş Bağcı
 
Dijkstra.ppt
Dijkstra.pptDijkstra.ppt
Dijkstra.ppt
Ruchika Sinha
 

What's hot (20)

Open Computer Vision Based Image Processing
Open Computer Vision Based Image ProcessingOpen Computer Vision Based Image Processing
Open Computer Vision Based Image Processing
 
Anlysis and design of algorithms part 1
Anlysis and design of algorithms part 1Anlysis and design of algorithms part 1
Anlysis and design of algorithms part 1
 
Thresholding.ppt
Thresholding.pptThresholding.ppt
Thresholding.ppt
 
Image Segmentation using Otsu's Method - Computer Graphics (UCS505) Project PPT
Image Segmentation using Otsu's Method - Computer Graphics (UCS505) Project PPTImage Segmentation using Otsu's Method - Computer Graphics (UCS505) Project PPT
Image Segmentation using Otsu's Method - Computer Graphics (UCS505) Project PPT
 
Unit 3 daa
Unit 3 daaUnit 3 daa
Unit 3 daa
 
Greedy algorithm
Greedy algorithmGreedy algorithm
Greedy algorithm
 
Histogram Equalization
Histogram EqualizationHistogram Equalization
Histogram Equalization
 
Graphics a buffer
Graphics a bufferGraphics a buffer
Graphics a buffer
 
Erosion and dilation
Erosion and dilationErosion and dilation
Erosion and dilation
 
Minimum spanning tree
Minimum spanning treeMinimum spanning tree
Minimum spanning tree
 
Deep Learning with PyTorch
Deep Learning with PyTorchDeep Learning with PyTorch
Deep Learning with PyTorch
 
10-SLR parser practice problems-02-06-2023.pptx
10-SLR parser practice problems-02-06-2023.pptx10-SLR parser practice problems-02-06-2023.pptx
10-SLR parser practice problems-02-06-2023.pptx
 
Morphological Image Processing
Morphological Image ProcessingMorphological Image Processing
Morphological Image Processing
 
Uninformed search
Uninformed searchUninformed search
Uninformed search
 
ImageProcessing10-Segmentation(Thresholding) (1).ppt
ImageProcessing10-Segmentation(Thresholding) (1).pptImageProcessing10-Segmentation(Thresholding) (1).ppt
ImageProcessing10-Segmentation(Thresholding) (1).ppt
 
adaboost
adaboostadaboost
adaboost
 
Graph Algorithms: Breadth-First Search (BFS)
Graph Algorithms: Breadth-First Search (BFS)Graph Algorithms: Breadth-First Search (BFS)
Graph Algorithms: Breadth-First Search (BFS)
 
PRIM'S ALGORITHM
PRIM'S ALGORITHMPRIM'S ALGORITHM
PRIM'S ALGORITHM
 
Lec7: Medical Image Segmentation (I) (Radiology Applications of Segmentation,...
Lec7: Medical Image Segmentation (I) (Radiology Applications of Segmentation,...Lec7: Medical Image Segmentation (I) (Radiology Applications of Segmentation,...
Lec7: Medical Image Segmentation (I) (Radiology Applications of Segmentation,...
 
Dijkstra.ppt
Dijkstra.pptDijkstra.ppt
Dijkstra.ppt
 

Viewers also liked

Introducing: A Complete Algebra of Data
Introducing: A Complete Algebra of DataIntroducing: A Complete Algebra of Data
Introducing: A Complete Algebra of Data
Inside Analysis
 
Building a website in Haskell coming from Node.js
Building a website in Haskell coming from Node.jsBuilding a website in Haskell coming from Node.js
Building a website in Haskell coming from Node.js
Nicolas Hery
 
Functional programming with haskell
Functional programming with haskellFunctional programming with haskell
Functional programming with haskell
faradjpour
 
Introduction to Nodejs and Isomorphic javascript
Introduction to Nodejs and Isomorphic javascriptIntroduction to Nodejs and Isomorphic javascript
Introduction to Nodejs and Isomorphic javascript
ChenKuo Chen
 
Fp example
Fp exampleFp example
Fp example
Laura Morris
 
Beam me up, Scotty
Beam me up, ScottyBeam me up, Scotty
Beam me up, Scotty
Gianluca Padovani
 
Scotty Cameron
Scotty CameronScotty Cameron
Scotty Cameron
Birdsey
 
You got ur Erlang in my Ruby
You got ur Erlang in my RubyYou got ur Erlang in my Ruby
You got ur Erlang in my Rubyelliando dias
 
Scotty Morrison Salesforce.com New HIre TED Talk: Behind the Brand
Scotty Morrison Salesforce.com New HIre TED Talk: Behind the BrandScotty Morrison Salesforce.com New HIre TED Talk: Behind the Brand
Scotty Morrison Salesforce.com New HIre TED Talk: Behind the Brand
Scotty Morrison
 
Scotty the Otter and his Adventure to You
Scotty the Otter and his Adventure to YouScotty the Otter and his Adventure to You
Scotty the Otter and his Adventure to You
ScottyOtty
 
Scotty, I need more speed - Koha tuning
Scotty, I need more speed - Koha tuningScotty, I need more speed - Koha tuning
Scotty, I need more speed - Koha tuning
Galen Charlton
 
Functional Programming and Haskell - TWBR Away Day 2011
Functional Programming and Haskell - TWBR Away Day 2011Functional Programming and Haskell - TWBR Away Day 2011
Functional Programming and Haskell - TWBR Away Day 2011
Adriano Bonat
 
Haskell
HaskellHaskell
Neo4j
Neo4jNeo4j
Neo4j
Von Stark
 
Introduction to Functional Programming with Haskell and JavaScript
Introduction to Functional Programming with Haskell and JavaScriptIntroduction to Functional Programming with Haskell and JavaScript
Introduction to Functional Programming with Haskell and JavaScript
Will Kurt
 
Functional programming seminar (haskell)
Functional programming seminar (haskell)Functional programming seminar (haskell)
Functional programming seminar (haskell)
Bikram Thapa
 
Introduction to haskell
Introduction to haskellIntroduction to haskell
Introduction to haskell
Luca Molteni
 
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
 
The other side of functional programming: Haskell for Erlang people
The other side of functional programming: Haskell for Erlang peopleThe other side of functional programming: Haskell for Erlang people
The other side of functional programming: Haskell for Erlang people
Bryan O'Sullivan
 
Haskell for the Real World
Haskell for the Real WorldHaskell for the Real World
Haskell for the Real World
Bryan O'Sullivan
 

Viewers also liked (20)

Introducing: A Complete Algebra of Data
Introducing: A Complete Algebra of DataIntroducing: A Complete Algebra of Data
Introducing: A Complete Algebra of Data
 
Building a website in Haskell coming from Node.js
Building a website in Haskell coming from Node.jsBuilding a website in Haskell coming from Node.js
Building a website in Haskell coming from Node.js
 
Functional programming with haskell
Functional programming with haskellFunctional programming with haskell
Functional programming with haskell
 
Introduction to Nodejs and Isomorphic javascript
Introduction to Nodejs and Isomorphic javascriptIntroduction to Nodejs and Isomorphic javascript
Introduction to Nodejs and Isomorphic javascript
 
Fp example
Fp exampleFp example
Fp example
 
Beam me up, Scotty
Beam me up, ScottyBeam me up, Scotty
Beam me up, Scotty
 
Scotty Cameron
Scotty CameronScotty Cameron
Scotty Cameron
 
You got ur Erlang in my Ruby
You got ur Erlang in my RubyYou got ur Erlang in my Ruby
You got ur Erlang in my Ruby
 
Scotty Morrison Salesforce.com New HIre TED Talk: Behind the Brand
Scotty Morrison Salesforce.com New HIre TED Talk: Behind the BrandScotty Morrison Salesforce.com New HIre TED Talk: Behind the Brand
Scotty Morrison Salesforce.com New HIre TED Talk: Behind the Brand
 
Scotty the Otter and his Adventure to You
Scotty the Otter and his Adventure to YouScotty the Otter and his Adventure to You
Scotty the Otter and his Adventure to You
 
Scotty, I need more speed - Koha tuning
Scotty, I need more speed - Koha tuningScotty, I need more speed - Koha tuning
Scotty, I need more speed - Koha tuning
 
Functional Programming and Haskell - TWBR Away Day 2011
Functional Programming and Haskell - TWBR Away Day 2011Functional Programming and Haskell - TWBR Away Day 2011
Functional Programming and Haskell - TWBR Away Day 2011
 
Haskell
HaskellHaskell
Haskell
 
Neo4j
Neo4jNeo4j
Neo4j
 
Introduction to Functional Programming with Haskell and JavaScript
Introduction to Functional Programming with Haskell and JavaScriptIntroduction to Functional Programming with Haskell and JavaScript
Introduction to Functional Programming with Haskell and JavaScript
 
Functional programming seminar (haskell)
Functional programming seminar (haskell)Functional programming seminar (haskell)
Functional programming seminar (haskell)
 
Introduction to haskell
Introduction to haskellIntroduction to haskell
Introduction to haskell
 
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!
 
The other side of functional programming: Haskell for Erlang people
The other side of functional programming: Haskell for Erlang peopleThe other side of functional programming: Haskell for Erlang people
The other side of functional programming: Haskell for Erlang people
 
Haskell for the Real World
Haskell for the Real WorldHaskell for the Real World
Haskell for the Real World
 

Similar to Haskell for data science

Building a Scalable Distributed Stats Infrastructure with Storm and KairosDB
Building a Scalable Distributed Stats Infrastructure with Storm and KairosDBBuilding a Scalable Distributed Stats Infrastructure with Storm and KairosDB
Building a Scalable Distributed Stats Infrastructure with Storm and KairosDB
Cody Ray
 
MongoDB 3.0
MongoDB 3.0 MongoDB 3.0
MongoDB 3.0
Victoria Malaya
 
Tim Popl
Tim PoplTim Popl
Tim Poplmchaar
 
The Next Mainstream Programming Language: A Game Developer’s Perspective
The Next Mainstream Programming Language: A Game Developer’s PerspectiveThe Next Mainstream Programming Language: A Game Developer’s Perspective
The Next Mainstream Programming Language: A Game Developer’s Perspective
guest4fd7a2
 
MXNet Workshop
MXNet WorkshopMXNet Workshop
MXNet Workshop
Amazon Web Services
 
Pune Clojure Course Outline
Pune Clojure Course OutlinePune Clojure Course Outline
Pune Clojure Course Outline
Baishampayan Ghose
 
Inferno Scalable Deep Learning on Spark
Inferno Scalable Deep Learning on SparkInferno Scalable Deep Learning on Spark
Inferno Scalable Deep Learning on Spark
DataWorks Summit/Hadoop Summit
 
Osd ctw spark
Osd ctw sparkOsd ctw spark
Osd ctw spark
Wisely chen
 
Apache Flink & Graph Processing
Apache Flink & Graph ProcessingApache Flink & Graph Processing
Apache Flink & Graph Processing
Vasia Kalavri
 
Performance and predictability (1)
Performance and predictability (1)Performance and predictability (1)
Performance and predictability (1)
RichardWarburton
 
Performance and Predictability - Richard Warburton
Performance and Predictability - Richard WarburtonPerformance and Predictability - Richard Warburton
Performance and Predictability - Richard Warburton
JAXLondon2014
 
NOSQL and Cassandra
NOSQL and CassandraNOSQL and Cassandra
NOSQL and Cassandrarantav
 
Fuel Up JavaScript with Functional Programming
Fuel Up JavaScript with Functional ProgrammingFuel Up JavaScript with Functional Programming
Fuel Up JavaScript with Functional Programming
Shine Xavier
 
Basic terminologies & asymptotic notations
Basic terminologies & asymptotic notationsBasic terminologies & asymptotic notations
Basic terminologies & asymptotic notations
Rajendran
 
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
 
Scala @ TechMeetup Edinburgh
Scala @ TechMeetup EdinburghScala @ TechMeetup Edinburgh
Scala @ TechMeetup Edinburgh
Stuart Roebuck
 
Natural Language Processing with CNTK and Apache Spark with Ali Zaidi
Natural Language Processing with CNTK and Apache Spark with Ali ZaidiNatural Language Processing with CNTK and Apache Spark with Ali Zaidi
Natural Language Processing with CNTK and Apache Spark with Ali Zaidi
Databricks
 
Concurrency Constructs Overview
Concurrency Constructs OverviewConcurrency Constructs Overview
Concurrency Constructs Overview
stasimus
 
Monads and Monoids by Oleksiy Dyagilev
Monads and Monoids by Oleksiy DyagilevMonads and Monoids by Oleksiy Dyagilev
Monads and Monoids by Oleksiy Dyagilev
JavaDayUA
 
Scala clojure techday_2011
Scala clojure techday_2011Scala clojure techday_2011
Scala clojure techday_2011
Thadeu Russo
 

Similar to Haskell for data science (20)

Building a Scalable Distributed Stats Infrastructure with Storm and KairosDB
Building a Scalable Distributed Stats Infrastructure with Storm and KairosDBBuilding a Scalable Distributed Stats Infrastructure with Storm and KairosDB
Building a Scalable Distributed Stats Infrastructure with Storm and KairosDB
 
MongoDB 3.0
MongoDB 3.0 MongoDB 3.0
MongoDB 3.0
 
Tim Popl
Tim PoplTim Popl
Tim Popl
 
The Next Mainstream Programming Language: A Game Developer’s Perspective
The Next Mainstream Programming Language: A Game Developer’s PerspectiveThe Next Mainstream Programming Language: A Game Developer’s Perspective
The Next Mainstream Programming Language: A Game Developer’s Perspective
 
MXNet Workshop
MXNet WorkshopMXNet Workshop
MXNet Workshop
 
Pune Clojure Course Outline
Pune Clojure Course OutlinePune Clojure Course Outline
Pune Clojure Course Outline
 
Inferno Scalable Deep Learning on Spark
Inferno Scalable Deep Learning on SparkInferno Scalable Deep Learning on Spark
Inferno Scalable Deep Learning on Spark
 
Osd ctw spark
Osd ctw sparkOsd ctw spark
Osd ctw spark
 
Apache Flink & Graph Processing
Apache Flink & Graph ProcessingApache Flink & Graph Processing
Apache Flink & Graph Processing
 
Performance and predictability (1)
Performance and predictability (1)Performance and predictability (1)
Performance and predictability (1)
 
Performance and Predictability - Richard Warburton
Performance and Predictability - Richard WarburtonPerformance and Predictability - Richard Warburton
Performance and Predictability - Richard Warburton
 
NOSQL and Cassandra
NOSQL and CassandraNOSQL and Cassandra
NOSQL and Cassandra
 
Fuel Up JavaScript with Functional Programming
Fuel Up JavaScript with Functional ProgrammingFuel Up JavaScript with Functional Programming
Fuel Up JavaScript with Functional Programming
 
Basic terminologies & asymptotic notations
Basic terminologies & asymptotic notationsBasic terminologies & asymptotic notations
Basic terminologies & asymptotic notations
 
The Fuss about || Haskell | Scala | F# ||
The Fuss about || Haskell | Scala | F# ||The Fuss about || Haskell | Scala | F# ||
The Fuss about || Haskell | Scala | F# ||
 
Scala @ TechMeetup Edinburgh
Scala @ TechMeetup EdinburghScala @ TechMeetup Edinburgh
Scala @ TechMeetup Edinburgh
 
Natural Language Processing with CNTK and Apache Spark with Ali Zaidi
Natural Language Processing with CNTK and Apache Spark with Ali ZaidiNatural Language Processing with CNTK and Apache Spark with Ali Zaidi
Natural Language Processing with CNTK and Apache Spark with Ali Zaidi
 
Concurrency Constructs Overview
Concurrency Constructs OverviewConcurrency Constructs Overview
Concurrency Constructs Overview
 
Monads and Monoids by Oleksiy Dyagilev
Monads and Monoids by Oleksiy DyagilevMonads and Monoids by Oleksiy Dyagilev
Monads and Monoids by Oleksiy Dyagilev
 
Scala clojure techday_2011
Scala clojure techday_2011Scala clojure techday_2011
Scala clojure techday_2011
 

Recently uploaded

一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
slg6lamcq
 
Analysis insight about a Flyball dog competition team's performance
Analysis insight about a Flyball dog competition team's performanceAnalysis insight about a Flyball dog competition team's performance
Analysis insight about a Flyball dog competition team's performance
roli9797
 
Enhanced Enterprise Intelligence with your personal AI Data Copilot.pdf
Enhanced Enterprise Intelligence with your personal AI Data Copilot.pdfEnhanced Enterprise Intelligence with your personal AI Data Copilot.pdf
Enhanced Enterprise Intelligence with your personal AI Data Copilot.pdf
GetInData
 
Machine learning and optimization techniques for electrical drives.pptx
Machine learning and optimization techniques for electrical drives.pptxMachine learning and optimization techniques for electrical drives.pptx
Machine learning and optimization techniques for electrical drives.pptx
balafet
 
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
u86oixdj
 
一比一原版(Dalhousie毕业证书)达尔豪斯大学毕业证如何办理
一比一原版(Dalhousie毕业证书)达尔豪斯大学毕业证如何办理一比一原版(Dalhousie毕业证书)达尔豪斯大学毕业证如何办理
一比一原版(Dalhousie毕业证书)达尔豪斯大学毕业证如何办理
mzpolocfi
 
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
Timothy Spann
 
Adjusting OpenMP PageRank : SHORT REPORT / NOTES
Adjusting OpenMP PageRank : SHORT REPORT / NOTESAdjusting OpenMP PageRank : SHORT REPORT / NOTES
Adjusting OpenMP PageRank : SHORT REPORT / NOTES
Subhajit Sahu
 
Unleashing the Power of Data_ Choosing a Trusted Analytics Platform.pdf
Unleashing the Power of Data_ Choosing a Trusted Analytics Platform.pdfUnleashing the Power of Data_ Choosing a Trusted Analytics Platform.pdf
Unleashing the Power of Data_ Choosing a Trusted Analytics Platform.pdf
Enterprise Wired
 
Adjusting primitives for graph : SHORT REPORT / NOTES
Adjusting primitives for graph : SHORT REPORT / NOTESAdjusting primitives for graph : SHORT REPORT / NOTES
Adjusting primitives for graph : SHORT REPORT / NOTES
Subhajit Sahu
 
原版制作(swinburne毕业证书)斯威本科技大学毕业证毕业完成信一模一样
原版制作(swinburne毕业证书)斯威本科技大学毕业证毕业完成信一模一样原版制作(swinburne毕业证书)斯威本科技大学毕业证毕业完成信一模一样
原版制作(swinburne毕业证书)斯威本科技大学毕业证毕业完成信一模一样
u86oixdj
 
Best best suvichar in gujarati english meaning of this sentence as Silk road ...
Best best suvichar in gujarati english meaning of this sentence as Silk road ...Best best suvichar in gujarati english meaning of this sentence as Silk road ...
Best best suvichar in gujarati english meaning of this sentence as Silk road ...
AbhimanyuSinha9
 
一比一原版(爱大毕业证书)爱丁堡大学毕业证如何办理
一比一原版(爱大毕业证书)爱丁堡大学毕业证如何办理一比一原版(爱大毕业证书)爱丁堡大学毕业证如何办理
一比一原版(爱大毕业证书)爱丁堡大学毕业证如何办理
g4dpvqap0
 
The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...
jerlynmaetalle
 
一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理
一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理
一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理
74nqk8xf
 
一比一原版(CBU毕业证)卡普顿大学毕业证如何办理
一比一原版(CBU毕业证)卡普顿大学毕业证如何办理一比一原版(CBU毕业证)卡普顿大学毕业证如何办理
一比一原版(CBU毕业证)卡普顿大学毕业证如何办理
ahzuo
 
Everything you wanted to know about LIHTC
Everything you wanted to know about LIHTCEverything you wanted to know about LIHTC
Everything you wanted to know about LIHTC
Roger Valdez
 
一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理
一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理
一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理
dwreak4tg
 
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Subhajit Sahu
 
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
Timothy Spann
 

Recently uploaded (20)

一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
 
Analysis insight about a Flyball dog competition team's performance
Analysis insight about a Flyball dog competition team's performanceAnalysis insight about a Flyball dog competition team's performance
Analysis insight about a Flyball dog competition team's performance
 
Enhanced Enterprise Intelligence with your personal AI Data Copilot.pdf
Enhanced Enterprise Intelligence with your personal AI Data Copilot.pdfEnhanced Enterprise Intelligence with your personal AI Data Copilot.pdf
Enhanced Enterprise Intelligence with your personal AI Data Copilot.pdf
 
Machine learning and optimization techniques for electrical drives.pptx
Machine learning and optimization techniques for electrical drives.pptxMachine learning and optimization techniques for electrical drives.pptx
Machine learning and optimization techniques for electrical drives.pptx
 
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
 
一比一原版(Dalhousie毕业证书)达尔豪斯大学毕业证如何办理
一比一原版(Dalhousie毕业证书)达尔豪斯大学毕业证如何办理一比一原版(Dalhousie毕业证书)达尔豪斯大学毕业证如何办理
一比一原版(Dalhousie毕业证书)达尔豪斯大学毕业证如何办理
 
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
 
Adjusting OpenMP PageRank : SHORT REPORT / NOTES
Adjusting OpenMP PageRank : SHORT REPORT / NOTESAdjusting OpenMP PageRank : SHORT REPORT / NOTES
Adjusting OpenMP PageRank : SHORT REPORT / NOTES
 
Unleashing the Power of Data_ Choosing a Trusted Analytics Platform.pdf
Unleashing the Power of Data_ Choosing a Trusted Analytics Platform.pdfUnleashing the Power of Data_ Choosing a Trusted Analytics Platform.pdf
Unleashing the Power of Data_ Choosing a Trusted Analytics Platform.pdf
 
Adjusting primitives for graph : SHORT REPORT / NOTES
Adjusting primitives for graph : SHORT REPORT / NOTESAdjusting primitives for graph : SHORT REPORT / NOTES
Adjusting primitives for graph : SHORT REPORT / NOTES
 
原版制作(swinburne毕业证书)斯威本科技大学毕业证毕业完成信一模一样
原版制作(swinburne毕业证书)斯威本科技大学毕业证毕业完成信一模一样原版制作(swinburne毕业证书)斯威本科技大学毕业证毕业完成信一模一样
原版制作(swinburne毕业证书)斯威本科技大学毕业证毕业完成信一模一样
 
Best best suvichar in gujarati english meaning of this sentence as Silk road ...
Best best suvichar in gujarati english meaning of this sentence as Silk road ...Best best suvichar in gujarati english meaning of this sentence as Silk road ...
Best best suvichar in gujarati english meaning of this sentence as Silk road ...
 
一比一原版(爱大毕业证书)爱丁堡大学毕业证如何办理
一比一原版(爱大毕业证书)爱丁堡大学毕业证如何办理一比一原版(爱大毕业证书)爱丁堡大学毕业证如何办理
一比一原版(爱大毕业证书)爱丁堡大学毕业证如何办理
 
The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...
 
一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理
一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理
一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理
 
一比一原版(CBU毕业证)卡普顿大学毕业证如何办理
一比一原版(CBU毕业证)卡普顿大学毕业证如何办理一比一原版(CBU毕业证)卡普顿大学毕业证如何办理
一比一原版(CBU毕业证)卡普顿大学毕业证如何办理
 
Everything you wanted to know about LIHTC
Everything you wanted to know about LIHTCEverything you wanted to know about LIHTC
Everything you wanted to know about LIHTC
 
一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理
一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理
一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理
 
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
 
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
 

Haskell for data science

Editor's Notes

  1. Introduction to speaker Contents
  2. Every value is immutable, every function result is deterministic, and without side-effects. Usually compiled to native machine code, mainly by GHC. Can also be compiled to javascript or interpreted. Statically typed. This gives the language a great deal of safety. No implicit conversion between values. Annoying for beginners In a strict language, functions require their arguments to be evaluated. In Haskell, all values are lazy by default, but explicit strictness is allowed.
  3. The Haskell wiki hints that there is a huge amount of proprietary, closed source financial code written in Haskell. Facebook have been doing a lot of functional programming, for instance React.js, which now has several Haskell implementations or wrappers. However, from what I’ve read, the main area they are using Haskell seems to be their data science team. BAE have used it. There are nearly 80 packages in the package index under the section Bioinformatics, more than most other sections. Oddly enough, Haskell is so underused that it’s easy to find top developers to work on your startup. Google, Microsoft, Intel, and NVidia have all used Haskell.
  4. Proof that it’s possible to throw something together quickly in Haskell
  5. What follows is a whirlwind tour of Haskell, skimming through the most important bits, and ignoring the rest of it.
  6. Putting expressions next to eachother applies the left one to the right one. In the case of multiple arguments, Haskell is left associative.
  7. This shows the type of the addition operator. The double colon denotes a type signature. Num a and anything to the left of a => is a type constraint. These allow us to be flexible about the types in our program, without compromising type safety
  8. You don’t have to apply a function to all of its arguments. Here, we’ve partially applied addition to the number 5, and the result was a function that accepts the remaining arguments.
  9. Higher order functions are functions that operate on or return other functions. Therefore all Haskell functions with arity > 1 are higher order functions. Map is a higher order function because it accepts a function as an argument. [a] denotes a list of type a. Here, we double all elements of a list by partially applying the multiplication operator to the number 2, then using map.
  10. Just wanted to mention the lambda syntax. The backslash is supposed to look like a lambda.
  11. This is equivalent to “class” in java/python or “defrecord” in clojure. ChessGame is the type, and NotStarted, PlayerTurn, and CheckMate are its constructors.
  12. A more convenient syntax for defining data types firstName, lastName, and personID are automatically declared as accessor functions I’ve also introduced a type variable s here. The type of Personrecord s depends on the type s. This might be useful because there are many different ways of representing a string in Haskell.
  13. _ = don’t care Pattern matching is useful for branching on different constructors and values. Pattern matching is useful for extracting fields for use in the function body Pattern matching is powerful. It works on lists and tuples Useful in many other places in Haskell
  14. Lists are just ordinary data definitions. They can be constructed by making an empty list, or by consing an element onto a list of similar elements. (:) is just a constructor. Recursive definition of map using pattern matching. At runtime, when map is applied, xs need not have been evaluated yet. This means that xs can be an infinite list! The recursion only stops when the caller stops evaluating the results that map returns. Since the result returned is in Weak Head Normal Form, f, and the recursive part of map are left unevaluated. Rarely have to define functions this way in real life. In most languages, this would be inefficient, and cause a stack overflow for lists of a certain size. In Haskell, unevaluated bindings are represented with thunks, which are lightweight and are stored on the heap.
  15. Suppose we define a data type to represent an AST for JSON….
  16. Typeclasses allow you to write type constraints. The purpose of them is polymorphism over types A typeclass is a set of types for which certain functions have been defined. The ‘instance’ declaration makes a type a member of a typeclass, and allows you to define those functions Types can be members of multiple typeclasses Typeclasses are open! Much more flexible than inheritance!
  17. Expressions can be represented as graphs, where each node is a value, or an unevaluated thunk. The next step is the application of graph reduction rules by the STG machine, which are based on the Lambda Calculus. The STG machine produces assembly language which carries out the operations required by these reductions. STG stands for Spineless Tagless G-machine, where G stands for graph. It lives in GHC and converts the lowest level of Haskell to Assembly language.
  18. Sharing is applied to avoid recomputing the same values
  19. The graph is reduced. Function application is a type of reduction. It’s more complicated than this in real life.
  20. Expressions can be represented as graphs, where each node is a value, or an unevaluated thunk. The next step is the application of graph reduction rules
  21. If the top level of a graph is a constructor, the graph is said to be in Weak Head Normal Form. This is used as the return value, and the lower levels are evaluated lazily, as needed. This is how laziness is implemented in Haskell
  22. This symbol is called Bottom. Bottom is an expression that can’t be evaluated, due to an infinite loop or an error. Bottom is a member of every type.
  23. This is pronounced “bind”
  24. In this slide I’m comparing monads to ordinary plain values. Monad is a typeclass whose instances define certain functions including >>= and return. The purpose of >>= is to allow us to provide a function that uses the plain value, and allow the Monad implementation to control how that function is called. We choose how to use the value inside the function, but the Monad’s implementation chooses how and when to call our function.
  25. Different Monad instances implement >>= differently, and will call your function in different ways.
  26. Can’t extract plain value in general. No way of using a and returning something of any other form than m b, in general.
  27. If you return a representation of your side effects, they are no longer really side-effects, they are the main return value of your function getLine reads a line from STDIN. The value it returns does not directly contain the value it has read, so it can be considered pure. Furthermore, chained, use of >>= constructs a chain of dependent computations. This guarantees the evaluation order.
  28. GHCI: Evaluate expressions, determine their types, inspect modules Hoogle: Search for functions by their type signature Cabal ~ a bit like make + package installer Hackage is a package DB. Like most language package databases, contains a large amount of unmaintained, disused, abandonware. Unlike most language-specific package databases, this abandonware has a good chance of still working! Fay, Haste, and GHCJS are Haskell to Javascript compilers.
  29. Neural nets, Markov models, SVMs, Hopfield network, Restricted Boltzmann Machines, a Convnet, genetic algorithms, dynamic time warping, Many different clustering algorithms, and Kalman filters. Unfortunately, none of the neural networks are capable of using the GPU yet, but one of my side projects is to build a deep learning library on top of Accelerate. The author of deeplearning-hs works for Facebook AI research and has experimented with Accelerate. dnngraph can generate models for Caffe and Torch.
  30. Many different packages for probability and statistics. You can also call R from Haskell
  31. No spark library unfortunately. Various libraries for interacting with Hadoop, but only two libraries for running Haskell on Hadoop Hadron. Hadron uses MapReduce streaming, and conduits. It requires Haskell to be installed on every node. Cloud Haskell is a distributed concurrency framework. There are also various connectors to the usual suspects like MySQL and PostgreSQL.
  32. Haskell has many graph plotting libraries. OpenGL and Haskell are an odd combination, but there are bindings, and they use Monads to represent the internal OpenGL state Haskell is very good for writing declarative DSLs. There are libraries for writing HTML and CSS using the do notation. The result is checked, and it looks very much like HAML or SASS.
  33. This generates a scatter plot and outputs it to a file. You could easily do this from GHCI
  34. Here’s some other the other plots this library is capable of. There are many other plotting libraries.
  35. We’ll generate a spherical mesh using recursion
  36. Octahedron. This is supposed to give you fairly even triangles. Turns out that if you start with a Tetrahedron, then after the first refinement on a given face, the middle triangle is twice the area of each of the other triangles. Also, Octahedrons are really easy to hardcode.
  37. The do notation uses the Maybe Monad. If a line returns Nothing, then the rest of the block is skipped. We’ll find the nearest neighbor and inspec the triangles it’s part of, binning the point into the nearest one. We’ll take a break now to look at some hacks and some problems we come across in Haskell
  38. Here are some functions and types that are considered harmful, but have their uses. Please use them carefully. undefined is bottom and if your program tries to evaluate it, it will crash at runtime. However, it can inhabit any type. This is useful for making dummy values to solve type errors in GHCI. unsafePerformIO extracts a value from an IO monad by performing the IO. Using this function can introduce impurity into pure functions, resulting in undefined behaviour IORef and MVar are mutable variables. Excessive use of these defeats the point of functional programming. unsafeCoerce changes the type of a value without changing the value. This can cause segfaults. trace wraps a unary function, printing out a string when it is evaluated
  39. Haskell has quite a steep learning curve, because it has too much confusing jargon and type system complications. Monad tutorial fallacy. People imagine that there’s a single explanation they helped them grasp a concept, whereas this explanation was actually the last one they read.
  40. Despite ensuring type correctness and guarding against race conditions with immutability, run time errors are still possible. You can cause runtime errors by incorrectly using the FFI, or by not covering all cases in a pattern match, or simply by throwing an exception
  41. If you don’t catch all cases in a pattern match, your program might bork at runtime. Annoyingly, this could be prevented at compile time, but only by setting a compiler flag
  42. Haskell has Exceptions
  43. error is sugar for undefined. It’s designed to represent a programming error, and bork with a message. Unfortunately it’s often used, so at some point you might need to catch it. Exceptions, however, are just data types, where the infrastructure required to throw and catch them is provided by a library. fail is a method which must be implemented by all monads. In the IO monad in Haskell 98, fail calls error, but the Maybe monad has a sensible implementation. fail’s presence in the language is generally regarded as a bad design decision and should be avoided. There are many abstractions for dealing with different errors and exceptions, and many differen monads. The monad transformer errorT is a good way of wrapping a monad with error and exception handling. Maybe and Either are elegant ways of handling computations that fail. They carry a result, or alternatively, some failure information. There are too many disparate ways of expressing and handling errors in Haskell.
  44. If you try and divide by 0, you get infinity rather than an error. Haskell could be safer if division returned a Maybe.
  45. If you really wanted to, you could redefine division to explicitly handle division by 0, and use -Wall to make you handle it at compile time.
  46. Space leaks are a problem. It’s very easy to consume crazy amounts of memory in Haskell.
  47. There are so many different types of space leak that they need their own zoo. You’ll probably come across that page at some point.
  48. Suppose we want to find the length of a lazy list. The elegant but naive implementation on the left is slow, leaks memory, and overflows the stack. The length of xs will be evaluated first, then the addition of 1. This causes traversal to the end of the list, building up thunks. When the end of the list is reached, the thunks must be evaluated in turn, then they can be freed. This is a lot of work just to perform simple addition. In the implementation on the right, we recurse last rather than first. len’ is tail recursive. This means that the recursive call is the last (or outermost) computation. This can be optimised into iteration which does not build a chain of thunks.
  49. Haven’t bothered with benchmarks, in case they might not be meaningful. For a given task, Haskell can be slower, because of unnecessary copying that might occur. However, due to laziness, it can skip unnecessary computation, so it’s possible that a Haskell implementation could be faster than a C/C++ implementation!
  50. Conduits are a way of piping data around in constant memory. Sources are conduits that only produce and Sinks are conduits that only consume. await consumes a value from the input, and yield produces a value. The conduit recurses on itself
  51. await consumes yield produces Recurse Turns out this leaks memory like a seive, then crashes, unless you have vast amounts of RAM, because of a bad choice of data structure
  52. Big Endian Patricia Trees are persistent data structures, so IntMap stores it’s entire state history!
  53. The moral of the story is, it’s sometimes OK to use mutable data structures in Haskell.
  54. Success! Triangular map of the density of heatmap points in OSM.
  55. This is how to multiply two matrices in Accelerate, on the GPU. It works by replicating the matrices into 3d arrays, and transposing one of them, so that matrix multiplication is an elementwise product and a summation. In practice, GPU cycles are not wasted replicating the matrices, thanks to fusion that takes place in Accelerate. One of my side projects is to implement symbolic differentiation for Accelerate, so that it’s easy to implement Deep learning, and you don’t have to spend time differentiating by hand when you want to add LSTMs or GRUs to your network.