SlideShare a Scribd company logo
1 of 36
Haskell for Scala-ists
        Chris Eidhof
Three things

• Datatypes
• Purity
• Laziness
List


•   data [a] = [] | a : [a]
List


 •   data [a] = [] | a : [a]


Type
List
       Type-parameter



 •   data [a] = [] | a : [a]


Type
List
       Type-parameter



 •   data [a] = [] | a : [a]


Type
               Constructor
List
       Type-parameter



 •   data [a] = [] | a : [a]

                               Case distinction
Type
               Constructor
List
       Type-parameter
                             Constructor



 •   data [a] = [] | a : [a]

                                Case distinction
Type
               Constructor
Functions

•   reverse :: [a] -> [a]

•   reverse []     = []

•   reverse (x:xs) = reverse xs ++ [x]
Functions
        Give me a list of a



•   reverse :: [a] -> [a]

•   reverse []        = []

•   reverse (x:xs) = reverse xs ++ [x]
Functions
        Give me a list of a And I’ll return a
                               list of a

•   reverse :: [a] -> [a]

•   reverse []        = []

•   reverse (x:xs) = reverse xs ++ [x]
Functions
        Give me a list of a And I’ll return a
                               list of a

•   reverse :: [a] -> [a]

•   reverse []          = []

•   reverse (x:xs) = reverse xs ++ [x]

     Pattern Matching
Purity



•   State = Evil
Purity

•    Same Input
•        =
•   Same output
Purity

•             Same Input
•       ntial
 Refere cy
Transp aren
                  =
•           Same output
Purity

• No side effects:
• * Variables / Mutable state
• * I/O
• * launchMissiles()
Example


•   sort :: [Int] -> [Int]


How do we know sort doesn’t launch missiles?
Doing I/O


•   putStrLn :: String -> IO ()
Doing I/O


•   putStrLn :: String -> IO ()


       The IO type shows us it’s not pure
Doing I/O
(>>=)   :: IO a -> (a -> IO b) -> IO b

main = putStrLn "hello! what's your name?"
   >>= ()   -> getLine
   >>= name -> putStrLn ("hello, " ++ name)
Doing I/O
(>>=)   :: IO a -> (a -> IO b) -> IO b

main = putStrLn "hello! what's your name?"
   >>= ()   -> getLine
   >>= name -> putStrLn ("hello, " ++ name)
Doing I/O


main = do
 putStrLn "hello! what's your name?"
 name <- getLine
 putStrLn ("hello, " ++ name)
Fusion



•   myFunction = map square . map toInt
Fusion



•   myFunction = map square . map toInt

•              = map (square . toInt)
Quickcheck


• Automatic testing of pure code.
Parallel code


•   map    :: (a -> b) -> [a] -> [b]

•   parMap :: (a -> b) -> [a] -> [b]
Composability

• Pure code is easy to compose, it is
• * Stateless
• * Self-contained
Effects
 Useful
              Most
            languages




                            Haskell

Useless
          Dangerous               Safe

   Simon Peyton-Jones, Caging The Effects Monster
Effects
 Useful
              Most            Nirvana
            languages




                            Haskell

Useless
          Dangerous               Safe

   Simon Peyton-Jones, Caging The Effects Monster
Effects
 Useful
              Most            Nirvana
            languages

                            Haskell




Useless
          Dangerous               Safe

   Simon Peyton-Jones, Caging The Effects Monster
Laziness


•   if (x < 10 && x > 5)
Laziness: Quicksort
qsort []     =    []
qsort (x:xs) =    qsort (filter (< x) xs)
             ++   [x]
             ++   qsort (filter (>= x) xs)

minimum ls = head (quickSort ls)
Laziness: Quicksort
qsort []     =    []
qsort (x:xs) =    qsort (filter (< x) xs)
             ++   [x]
             ++   qsort (filter (>= x) xs)

minimum ls = head (quickSort ls)
Read more
• Real World Haskell - http://book.realworldhaskell.org/
• Haskell.org - http://haskell.org
• Haskell Café - http://haskell.org/haskellwiki/Mailing_lists
• Planet Haskell - http://planet.haskell.org/
• Haskell reddit - http://haskell.reddit.com
•
Getting Started

• 1. Install the Haskell Platform
• http://hackage.haskell.org/platform/
• 2. Haskell in 10 minutes
• http://haskell.org/haskellwiki/
  Learn_Haskell_in_10_minutes
Keep in touch


• http://github.com/chriseidhof
• @chriseidhof

More Related Content

What's hot

Swift rocks! #1
Swift rocks! #1Swift rocks! #1
Swift rocks! #1Hackraft
 
Swift Rocks #2: Going functional
Swift Rocks #2: Going functionalSwift Rocks #2: Going functional
Swift Rocks #2: Going functionalHackraft
 
Java Polymorphism Part 2
Java Polymorphism Part 2Java Polymorphism Part 2
Java Polymorphism Part 2AathikaJava
 
Scala: Functioneel programmeren in een object georiënteerde wereld
Scala: Functioneel programmeren in een object georiënteerde wereldScala: Functioneel programmeren in een object georiënteerde wereld
Scala: Functioneel programmeren in een object georiënteerde wereldWerner Hofstra
 
Yin Yangs of Software Development
Yin Yangs of Software DevelopmentYin Yangs of Software Development
Yin Yangs of Software DevelopmentNaveenkumar Muguda
 
Exploring ZIO Prelude: The game changer for typeclasses in Scala
Exploring ZIO Prelude: The game changer for typeclasses in ScalaExploring ZIO Prelude: The game changer for typeclasses in Scala
Exploring ZIO Prelude: The game changer for typeclasses in ScalaJorge Vásquez
 
An introduction to scala
An introduction to scalaAn introduction to scala
An introduction to scalaXing
 
Quark: A Purely-Functional Scala DSL for Data Processing & Analytics
Quark: A Purely-Functional Scala DSL for Data Processing & AnalyticsQuark: A Purely-Functional Scala DSL for Data Processing & Analytics
Quark: A Purely-Functional Scala DSL for Data Processing & AnalyticsJohn De Goes
 
The Ring programming language version 1.7 book - Part 39 of 196
The Ring programming language version 1.7 book - Part 39 of 196The Ring programming language version 1.7 book - Part 39 of 196
The Ring programming language version 1.7 book - Part 39 of 196Mahmoud Samir Fayed
 
Introduction to Simple.Data
Introduction to Simple.DataIntroduction to Simple.Data
Introduction to Simple.DataTim Bourguignon
 
The Ring programming language version 1.8 book - Part 37 of 202
The Ring programming language version 1.8 book - Part 37 of 202The Ring programming language version 1.8 book - Part 37 of 202
The Ring programming language version 1.8 book - Part 37 of 202Mahmoud Samir Fayed
 
.Net Collection Classes Deep Dive - Rocksolid Tour 2013
.Net Collection Classes Deep Dive  - Rocksolid Tour 2013.Net Collection Classes Deep Dive  - Rocksolid Tour 2013
.Net Collection Classes Deep Dive - Rocksolid Tour 2013Gary Short
 
Why Scala is the better Java
Why Scala is the better JavaWhy Scala is the better Java
Why Scala is the better JavaThomas Kaiser
 
Front end fundamentals session 1: javascript core
Front end fundamentals session 1: javascript coreFront end fundamentals session 1: javascript core
Front end fundamentals session 1: javascript coreWeb Zhao
 
2.1 Recap From Day One
2.1 Recap From Day One2.1 Recap From Day One
2.1 Recap From Day Oneretronym
 

What's hot (18)

Swift rocks! #1
Swift rocks! #1Swift rocks! #1
Swift rocks! #1
 
Swift Rocks #2: Going functional
Swift Rocks #2: Going functionalSwift Rocks #2: Going functional
Swift Rocks #2: Going functional
 
Java Polymorphism Part 2
Java Polymorphism Part 2Java Polymorphism Part 2
Java Polymorphism Part 2
 
Scala: Functioneel programmeren in een object georiënteerde wereld
Scala: Functioneel programmeren in een object georiënteerde wereldScala: Functioneel programmeren in een object georiënteerde wereld
Scala: Functioneel programmeren in een object georiënteerde wereld
 
Yin Yangs of Software Development
Yin Yangs of Software DevelopmentYin Yangs of Software Development
Yin Yangs of Software Development
 
Exploring ZIO Prelude: The game changer for typeclasses in Scala
Exploring ZIO Prelude: The game changer for typeclasses in ScalaExploring ZIO Prelude: The game changer for typeclasses in Scala
Exploring ZIO Prelude: The game changer for typeclasses in Scala
 
An introduction to scala
An introduction to scalaAn introduction to scala
An introduction to scala
 
Quark: A Purely-Functional Scala DSL for Data Processing & Analytics
Quark: A Purely-Functional Scala DSL for Data Processing & AnalyticsQuark: A Purely-Functional Scala DSL for Data Processing & Analytics
Quark: A Purely-Functional Scala DSL for Data Processing & Analytics
 
The Ring programming language version 1.7 book - Part 39 of 196
The Ring programming language version 1.7 book - Part 39 of 196The Ring programming language version 1.7 book - Part 39 of 196
The Ring programming language version 1.7 book - Part 39 of 196
 
Introduction to Simple.Data
Introduction to Simple.DataIntroduction to Simple.Data
Introduction to Simple.Data
 
Python3
Python3Python3
Python3
 
The Ring programming language version 1.8 book - Part 37 of 202
The Ring programming language version 1.8 book - Part 37 of 202The Ring programming language version 1.8 book - Part 37 of 202
The Ring programming language version 1.8 book - Part 37 of 202
 
.Net Collection Classes Deep Dive - Rocksolid Tour 2013
.Net Collection Classes Deep Dive  - Rocksolid Tour 2013.Net Collection Classes Deep Dive  - Rocksolid Tour 2013
.Net Collection Classes Deep Dive - Rocksolid Tour 2013
 
Why Scala is the better Java
Why Scala is the better JavaWhy Scala is the better Java
Why Scala is the better Java
 
Front end fundamentals session 1: javascript core
Front end fundamentals session 1: javascript coreFront end fundamentals session 1: javascript core
Front end fundamentals session 1: javascript core
 
Java naming conventions
Java naming conventionsJava naming conventions
Java naming conventions
 
MTL Versus Free
MTL Versus FreeMTL Versus Free
MTL Versus Free
 
2.1 Recap From Day One
2.1 Recap From Day One2.1 Recap From Day One
2.1 Recap From Day One
 

Similar to Haskell for Scala-ists

Programming Haskell Chapter8
Programming Haskell Chapter8Programming Haskell Chapter8
Programming Haskell Chapter8Kousuke Ruichi
 
Why Haskell Matters
Why Haskell MattersWhy Haskell Matters
Why Haskell Mattersromanandreg
 
Sequence and Traverse - Part 3
Sequence and Traverse - Part 3Sequence and Traverse - Part 3
Sequence and Traverse - Part 3Philip Schwarz
 
Five Languages in a Moment
Five Languages in a MomentFive Languages in a Moment
Five Languages in a MomentSergio Gil
 
Monads and Monoids by Oleksiy Dyagilev
Monads and Monoids by Oleksiy DyagilevMonads and Monoids by Oleksiy Dyagilev
Monads and Monoids by Oleksiy DyagilevJavaDayUA
 
Datastructures in python
Datastructures in pythonDatastructures in python
Datastructures in pythonhydpy
 
Invertible-syntax 入門
Invertible-syntax 入門Invertible-syntax 入門
Invertible-syntax 入門Hiromi Ishii
 
Dynamic languages, for software craftmanship group
Dynamic languages, for software craftmanship groupDynamic languages, for software craftmanship group
Dynamic languages, for software craftmanship groupReuven Lerner
 
Learning notes of r for python programmer (Temp1)
Learning notes of r for python programmer (Temp1)Learning notes of r for python programmer (Temp1)
Learning notes of r for python programmer (Temp1)Chia-Chi Chang
 
OSCON Presentation: Developing High Performance Websites and Modern Apps with...
OSCON Presentation: Developing High Performance Websites and Modern Apps with...OSCON Presentation: Developing High Performance Websites and Modern Apps with...
OSCON Presentation: Developing High Performance Websites and Modern Apps with...Doris Chen
 
Happy Go Programming
Happy Go ProgrammingHappy Go Programming
Happy Go ProgrammingLin Yo-An
 
Python Programming for basic beginners.pptx
Python Programming for basic beginners.pptxPython Programming for basic beginners.pptx
Python Programming for basic beginners.pptxmohitesoham12
 
Data Analysis and Programming in R
Data Analysis and Programming in RData Analysis and Programming in R
Data Analysis and Programming in REshwar Sai
 
Fire in the type hole
Fire in the type holeFire in the type hole
Fire in the type holeihji
 
Oleksii Levzhynskyi "Solving behavioral complexity with FRP"
Oleksii Levzhynskyi "Solving behavioral complexity with FRP"Oleksii Levzhynskyi "Solving behavioral complexity with FRP"
Oleksii Levzhynskyi "Solving behavioral complexity with FRP"Fwdays
 
Introduction to R
Introduction to RIntroduction to R
Introduction to Rvpletap
 
An overview of Python 2.7
An overview of Python 2.7An overview of Python 2.7
An overview of Python 2.7decoupled
 

Similar to Haskell for Scala-ists (20)

P3 2018 python_regexes
P3 2018 python_regexesP3 2018 python_regexes
P3 2018 python_regexes
 
Programming Haskell Chapter8
Programming Haskell Chapter8Programming Haskell Chapter8
Programming Haskell Chapter8
 
Why Haskell Matters
Why Haskell MattersWhy Haskell Matters
Why Haskell Matters
 
Sequence and Traverse - Part 3
Sequence and Traverse - Part 3Sequence and Traverse - Part 3
Sequence and Traverse - Part 3
 
Five Languages in a Moment
Five Languages in a MomentFive Languages in a Moment
Five Languages in a Moment
 
Monads and Monoids by Oleksiy Dyagilev
Monads and Monoids by Oleksiy DyagilevMonads and Monoids by Oleksiy Dyagilev
Monads and Monoids by Oleksiy Dyagilev
 
Datastructures in python
Datastructures in pythonDatastructures in python
Datastructures in python
 
Invertible-syntax 入門
Invertible-syntax 入門Invertible-syntax 入門
Invertible-syntax 入門
 
Dynamic languages, for software craftmanship group
Dynamic languages, for software craftmanship groupDynamic languages, for software craftmanship group
Dynamic languages, for software craftmanship group
 
Learning notes of r for python programmer (Temp1)
Learning notes of r for python programmer (Temp1)Learning notes of r for python programmer (Temp1)
Learning notes of r for python programmer (Temp1)
 
OSCON Presentation: Developing High Performance Websites and Modern Apps with...
OSCON Presentation: Developing High Performance Websites and Modern Apps with...OSCON Presentation: Developing High Performance Websites and Modern Apps with...
OSCON Presentation: Developing High Performance Websites and Modern Apps with...
 
Happy Go Programming
Happy Go ProgrammingHappy Go Programming
Happy Go Programming
 
Python Programming for basic beginners.pptx
Python Programming for basic beginners.pptxPython Programming for basic beginners.pptx
Python Programming for basic beginners.pptx
 
Data Analysis and Programming in R
Data Analysis and Programming in RData Analysis and Programming in R
Data Analysis and Programming in R
 
Fire in the type hole
Fire in the type holeFire in the type hole
Fire in the type hole
 
Oleksii Levzhynskyi "Solving behavioral complexity with FRP"
Oleksii Levzhynskyi "Solving behavioral complexity with FRP"Oleksii Levzhynskyi "Solving behavioral complexity with FRP"
Oleksii Levzhynskyi "Solving behavioral complexity with FRP"
 
Perl_Tutorial_v1
Perl_Tutorial_v1Perl_Tutorial_v1
Perl_Tutorial_v1
 
Perl_Tutorial_v1
Perl_Tutorial_v1Perl_Tutorial_v1
Perl_Tutorial_v1
 
Introduction to R
Introduction to RIntroduction to R
Introduction to R
 
An overview of Python 2.7
An overview of Python 2.7An overview of Python 2.7
An overview of Python 2.7
 

Haskell for Scala-ists

  • 1. Haskell for Scala-ists Chris Eidhof
  • 2. Three things • Datatypes • Purity • Laziness
  • 3. List • data [a] = [] | a : [a]
  • 4. List • data [a] = [] | a : [a] Type
  • 5. List Type-parameter • data [a] = [] | a : [a] Type
  • 6. List Type-parameter • data [a] = [] | a : [a] Type Constructor
  • 7. List Type-parameter • data [a] = [] | a : [a] Case distinction Type Constructor
  • 8. List Type-parameter Constructor • data [a] = [] | a : [a] Case distinction Type Constructor
  • 9. Functions • reverse :: [a] -> [a] • reverse [] = [] • reverse (x:xs) = reverse xs ++ [x]
  • 10. Functions Give me a list of a • reverse :: [a] -> [a] • reverse [] = [] • reverse (x:xs) = reverse xs ++ [x]
  • 11. Functions Give me a list of a And I’ll return a list of a • reverse :: [a] -> [a] • reverse [] = [] • reverse (x:xs) = reverse xs ++ [x]
  • 12. Functions Give me a list of a And I’ll return a list of a • reverse :: [a] -> [a] • reverse [] = [] • reverse (x:xs) = reverse xs ++ [x] Pattern Matching
  • 13. Purity • State = Evil
  • 14. Purity • Same Input • = • Same output
  • 15. Purity • Same Input • ntial Refere cy Transp aren = • Same output
  • 16. Purity • No side effects: • * Variables / Mutable state • * I/O • * launchMissiles()
  • 17. Example • sort :: [Int] -> [Int] How do we know sort doesn’t launch missiles?
  • 18. Doing I/O • putStrLn :: String -> IO ()
  • 19. Doing I/O • putStrLn :: String -> IO () The IO type shows us it’s not pure
  • 20. Doing I/O (>>=) :: IO a -> (a -> IO b) -> IO b main = putStrLn "hello! what's your name?" >>= () -> getLine >>= name -> putStrLn ("hello, " ++ name)
  • 21. Doing I/O (>>=) :: IO a -> (a -> IO b) -> IO b main = putStrLn "hello! what's your name?" >>= () -> getLine >>= name -> putStrLn ("hello, " ++ name)
  • 22. Doing I/O main = do putStrLn "hello! what's your name?" name <- getLine putStrLn ("hello, " ++ name)
  • 23. Fusion • myFunction = map square . map toInt
  • 24. Fusion • myFunction = map square . map toInt • = map (square . toInt)
  • 26. Parallel code • map  :: (a -> b) -> [a] -> [b] • parMap :: (a -> b) -> [a] -> [b]
  • 27. Composability • Pure code is easy to compose, it is • * Stateless • * Self-contained
  • 28. Effects Useful Most languages Haskell Useless Dangerous Safe Simon Peyton-Jones, Caging The Effects Monster
  • 29. Effects Useful Most Nirvana languages Haskell Useless Dangerous Safe Simon Peyton-Jones, Caging The Effects Monster
  • 30. Effects Useful Most Nirvana languages Haskell Useless Dangerous Safe Simon Peyton-Jones, Caging The Effects Monster
  • 31. Laziness • if (x < 10 && x > 5)
  • 32. Laziness: Quicksort qsort [] = [] qsort (x:xs) = qsort (filter (< x) xs) ++ [x] ++ qsort (filter (>= x) xs) minimum ls = head (quickSort ls)
  • 33. Laziness: Quicksort qsort [] = [] qsort (x:xs) = qsort (filter (< x) xs) ++ [x] ++ qsort (filter (>= x) xs) minimum ls = head (quickSort ls)
  • 34. Read more • Real World Haskell - http://book.realworldhaskell.org/ • Haskell.org - http://haskell.org • Haskell Café - http://haskell.org/haskellwiki/Mailing_lists • Planet Haskell - http://planet.haskell.org/ • Haskell reddit - http://haskell.reddit.com •
  • 35. Getting Started • 1. Install the Haskell Platform • http://hackage.haskell.org/platform/ • 2. Haskell in 10 minutes • http://haskell.org/haskellwiki/ Learn_Haskell_in_10_minutes
  • 36. Keep in touch • http://github.com/chriseidhof • @chriseidhof

Editor's Notes