Haskell 101
Haskell 101
ibobyr@, nicuveo@
go/haskell-101-slides-mtv
▶ 101
▶ Concepts and generalities
▶ Syntax overview
▶ Data structures
▶ Declaring functions
1 / 32
Not today
▶ Project environment
▶ Cabal?
▶ Stackage? Stack?
▶ Haskell in Google3?
▶ Advanced stuff
▶ Functors?
▶ Monads?
▶ Monad Transformers?
2 / 32
Prerequisites
▶ Programming knowledge
▶ Functional programming is a plus
▶ Imperative programming is enough
▶ Compiler
▶ apt-get install haskell-platform
3 / 32
GHCI is your new best friend
4 / 32
Haskell is...
5 / 32
Haskell is NOT...
▶ A silver bullet
▶ Only for category theorists
6 / 32
Haskell is NOT...
▶ A silver bullet
▶ Only for category theorists
▶ Hard!
6 / 32
Haskell is NOT...
▶ A silver bullet
▶ Only for category theorists
▶ Hard! Just different...
6 / 32
Purely functional
7 / 32
Purely functional
1 + 2
7 / 32
Purely functional
let a = 1
in a + 2
7 / 32
Purely functional
7 / 32
Purely functional
7 / 32
Purely functional
7 / 32
Purely functional
let a = 3
in a = a + 1 -- compile error
7 / 32
Purely functional
7 / 32
Purely functional
7 / 32
Purity...
8 / 32
Purity...
8 / 32
Purity...
8 / 32
...and corruption
9 / 32
...and corruption
IO corrupts.
9 / 32
...and corruption
IO corrupts.
9 / 32
Lazy
10 / 32
Lazy
Quiz
10 / 32
Lazy
(C++)
if (obj != NULL && obj->value > 0)
10 / 32
Reduction steps
add( x , y ) = x + y
11 / 32
Reduction steps
add( x , y ) = x + y
add( 12 + 8 , 20 + 2 )
11 / 32
Reduction steps
add( x , y ) = x + y
add( 12 + 8 , 20 + 2 )
add( 20 , 22 )
11 / 32
Reduction steps
add( x , y ) = x + y
add( 12 + 8 , 20 + 2 )
add( 20 , 22 )
20 + 22
11 / 32
Reduction steps
add( x , y ) = x + y
add( 12 + 8 , 20 + 2 )
add( 20 , 22 )
20 + 22
42
11 / 32
Reduction steps
11 / 32
Reduction steps
add( 12 + 8 , 20 + 2 )
11 / 32
Reduction steps
add( 12 + 8 , 20 + 2 )
12 + 8 + 20 + 2
11 / 32
Reduction steps
add( 12 + 8 , 20 + 2 )
12 + 8 + 20 + 2
42
11 / 32
Laziness pros and cons
- Memory pitfalls
12 / 32
Laziness pros and cons
- Memory pitfalls
- IO and parallelism pitfalls
12 / 32
Laziness pros and cons
- Memory pitfalls
- IO and parallelism pitfalls
+ Huge optimizations
12 / 32
Laziness pros and cons
- Memory pitfalls
- IO and parallelism pitfalls
+ Huge optimizations
+ Greater expressivity (e.g. infinite structures)
12 / 32
Function types
13 / 32
Function types
13 / 32
Function types
13 / 32
Function types
13 / 32
Codelab - Section 1
▶ Codelab - Section 1
▶ Compiler
▶ apt-get install haskell-platform
▶ Source
▶ git clone \
https://github.com/google/haskell-trainings.git
cd haskell-trainings/haskell_101/codelab
▶ go/haskell-101-codelab-mtv
▶ Zip on X20
/google/data/ro/users/ib/ibobyr/haskell_101_codelab.zip
▶ Piper
//google3/experimental/users/ibobyr
haskell/grow/101/codelab
14 / 32
Curried functions, partial application
15 / 32
Curried functions, partial application
15 / 32
Curried functions, partial application
15 / 32
Curried functions, partial application
(f 1) 2 :: Int
15 / 32
Curried functions, partial application
15 / 32
Quizz!
16 / 32
Quizz!
16 / 32
Quizz!
16 / 32
Quizz!
16 / 32
Quizz!
16 / 32
Quizz!
16 / 32
Quizz!
16 / 32
Quizz!
let a = fun (x + y)
16 / 32
Quizz!
let a = fun $ x + y
16 / 32
Quizz!
16 / 32
Quizz!
(f ◦ g)(x) = f(g(x))
16 / 32
Quizz!
16 / 32
Quizz!
16 / 32
Quizz with a vengeance!
17 / 32
Quizz with a vengeance!
17 / 32
Quizz with a vengeance!
”reduce”
17 / 32
Algebraic data types
▶ Type composition
▶ Product and sum types
▶ Cardinality expressions
18 / 32
Type synonyms
19 / 32
Type synonyms
19 / 32
Type synonyms
19 / 32
Immutable data structures...
20 / 32
Immutable data structures...
20 / 32
Immutable data structures...
20 / 32
Immutable data structures...
20 / 32
Immutable data structures...
What’s left?
20 / 32
Immutable data structures...
20 / 32
The almighty ”data” keyword
None :: None
21 / 32
The almighty ”data” keyword
21 / 32
The almighty ”data” keyword
True :: Bool
False :: Bool
21 / 32
Not
22 / 32
Not
22 / 32
Not
22 / 32
Not
#PatternMatching
22 / 32
And
23 / 32
And
23 / 32
And
23 / 32
And
23 / 32
And
23 / 32
And
23 / 32
And
23 / 32
And
23 / 32
And
23 / 32
Deconstructors?
24 / 32
Deconstructors?
24 / 32
Deconstructors?
24 / 32
Deconstructors?
24 / 32
Deconstructors?
24 / 32
Codelab - Section 2
▶ Codelab - Section 2
25 / 32
The almighty ”data” keyword. Continued
26 / 32
The almighty ”data” keyword. Continued
Nothing :: Maybe a
Just :: a -> Maybe a
Just 42 :: Maybe Int
26 / 32
The almighty ”data” keyword. Continued
Nil :: List a
Cell :: a -> List a -> List a
Cell 0 (Cell 1 (Nil)) :: List Int
26 / 32
The almighty ”data” keyword. Continued
Nil :: List a
Cell :: a -> List a -> List a
Cell 0 $ Cell 1 $ Nil :: List Int
26 / 32
The almighty ”data” keyword. Continued
[] :: [a]
(:) :: a -> [a] -> [a]
0:1:[] :: [Int]
26 / 32
The almighty ”data” keyword. Continued
[] :: [a]
(:) :: a -> [a] -> [a]
[0,1] :: [Int]
26 / 32
Record syntax
27 / 32
Record syntax
27 / 32
Record syntax
27 / 32
Length
28 / 32
Length
28 / 32
Length
28 / 32
Length
#Recursion
28 / 32
The end of the theoretical part!
Questions?
29 / 32
Links
▶ go/haskell-101-slides-mtv
▶ tryhaskell.org
▶ learnyouahaskell.com
▶ book.realworldhaskell.org
▶ haskellbook.com
▶ haskell.org/hoogle/
▶ Pragmatic types: types vs tests
30 / 32
Codelab - Sections 3, 4, 5, and 6
31 / 32
The end!
Questions?
32 / 32