Professional Documents
Culture Documents
Haskell
2011-05-19
Introduction to Weird Functional Languages with Haskell
Table of Contents
Functional Programming
The Functional Paradigm
Haskell
Basic Syntax
Custom Data Structures
Typeclasses
List
Pattern Matching
Binary Tree
Think Functionally
Common Patterns
Recursion with Lists
Functors
Functions as Arguments
Applicative Functors
Higher Order Functions
Monoids
Lambdas
Future Topics
Currying
Where to go to Learn More
Flip
Function Composition
Laziness
Functional Programming
Functional Programming
Functional Programming – The Functional Paradigm
I Declarative
I First Class Functions
I Pure Functions
I Immutibility
I Parallelism
Functional Programming – The Functional Paradigm
Declarative
I “What” instead of “How”
I Smart Compilers
Pure Functions
I No I/O or Modification of State
I Safety
I Optimization
Functional Programming – The Functional Paradigm
Immutability
I Can’t Change Data
Parallelism
I Locks Unnecessary
Extra Stuff
I Code Generation with Lisp
Haskell
Haskell – Basic Syntax
Functions
I True and False
I [1, 2, 3]
I (1, 3)
I (+), sum
Haskell – Basic Syntax
Function Signatures
I 12 :: Int
I [1, 2, 3] :: [Integer]
| 0 | 1 | 2 | ... | 2147483647
I data Color = Red | Green | Blue
Function Definitions
I even :: Integer -> Integer -> Bool
even x = (mod x 2) == 0
I odd :: Integer -> Integer -> Bool
The Problem
I Function Scope
The Solution
I Ad-hoc polymorphic interfaces
I class Eq a where
The Win
I Typeclass Deriving
I Implement Show
I Implement (==)
Haskell – Pattern Matching
I val2 = Nothing
possiblyDouble x = undefined
Haskell – Pattern Matching
I val2 = Nothing
I val2 = Nothing
possibleDouble x = case x of
Nothing -> Nothing
(Just x) -> x + x
Haskell – Pattern Matching
Guards
I val1 = 5
I val2 = 9001
doubleSmall x
| x <= 9000 = x + x
| otherwise = x
Think Functionally
Think Functionally
Think Functionally – Recursion with Lists
I myList = [1,2,3,4,5]
I sum :: [Int] -> Int -> Int
sum [] acc = acc
sum [x:xs] acc = sum xs (acc + x)
Think Functionally – Functions as Arguments
I myList = [1,2,3,4,5]
I double :: Int -> Int
I map :: (a -> b) -> [a] -> [b]
Think Functionally – Functions as Arguments
I example = “things”
I toUpper :: Char -> Char
I map :: (a -> b) -> [a] -> [b]
Think Functionally – Functions as Arguments
I myList = [1,2,3,4,5]
I foldl :: (a -> b -> a) -> a -> [b] -> a
I sum = ?
Think Functionally – Higher Order Functions
I myList = [1,2,3,4,5]
I triple = map (\x -> x + x + x) myList
I divisThree = filter (\x -> (mod x 3) == 0)
myList
Think Functionally – Currying
I ghci> :t (+)
(+) :: Num a => a -> a -> a
ghci> :t (+ 1)
(+ 1) :: Num a => a -> a
I applyTwice :: (a -> a) -> a -> a
applyTwice f x = f (f x)
Think Functionally – Flip
I “undefined”
I Circular Structures
I Unique to Haskell
Think Functionally – Laziness
I fac 0 = 1
fac n = n * fac (n - 1)
I fib = 0 : 1 : zipWith (+) fib (tail fib)
Think Functionally – Laziness
undefined
I Valid as long as unevaluated
Circular Structures
I No mutable references
circularFoo = x
where x = Bar 1 y
y = Bar 2 x
I Sci-Fi-Explanation: “You can borrow things
Common Patterns
Common Patterns – Functors
I Recall Currying...
I plusX m = fmap plux m
where plux x = (+ x)
I uh oh...
I ghci> :t plusX [1,2,3,4,5]
plusX [1,2,3,4,5] :: Num a => [a -> a]
Common Patterns – Applicative Functors
Monoids
I class Monoid a where
mempty = []
mappend = (++)
mconcat = foldr mappend mempty
Common Patterns – Monoids
I Testing/Quickcheck
I Error Handling
I Mutable Objects and Arrays
I Parallel Programming and STM
I Functional Reactive Programming
I Foreign Function Interface
I Cabal and Hackage
I Examine Real Code
I XMonad Window Manager (<1000 lines)
I Parsec
I Yesod Web Framework
I Darcs, Version Control System
I The Glorious Glasgow Haskell Compiler
I Agda Theorem Prover
Where to go to Learn More
I Haskell Wikibook 3
1
HTML:
http://learnyouahaskell.com/
2
HTML:
http://book.realworldhaskell.org/
3
HTML:
https://secure.wikimedia.org/wikibooks/en/wiki/Haskell
4
HTML:
http://halogen.note.amherst.edu/~jdtang/scheme_in_48/tutorial/
overview.html
Where to go to Learn More
1
HTML:
http://hackage.haskell.org/
2
HTML:
http://haskell.org/hoogle/
3
HTML:
http://holumbus.fh-wedel.de/hayoo/hayoo.html
4
HTML:
http://www.haskell.org/haskellwiki/IRC_channel
Thanks
Thanks
Thanks