Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

An Introduction to Functional Programming using Haskell

4,235 views

Published on

Slides of the workshop I gave at Devnology's Community Day.

Published in: Technology, Education
  • Be the first to comment

An Introduction to Functional Programming using Haskell

  1. 1. An Introduction to Functional Programming using Haskell Michel Rijnders <mies@tty.nl>
  2. 2. Administrativia Anglais? tuple programming skill level working ghci handouts breaks
  3. 3. Main Features purely functional lazy higher order strongly typed general purpose
  4. 4. History September 1987 FPCA “design by committee” P. Hudak, J. Hughes, S. Peyton Jones, and P. Wadler: “A History of Haskell: Being Lazy With Class” (2007)
  5. 5. Main Difference mainstream languages are all about state functional programming is all about values
  6. 6. Computation all computation is done via the evaluation of EXPRESSIONS to yield VALUES every value has an associated TYPE
  7. 7. Types basic types: Char, Bool, Int, Integer, Double composite types lists: [Char], [Int], [[Char]] tuples: (String,Int)
  8. 8. Function Types square :: Integer -> Integer (&&) :: Bool -> Bool -> Bool length :: [a] -> Int (:) :: a -> [a] -> [a]
  9. 9. Function Definitions fac :: Int -> Int fac n = if n == 0 then 1 else n * fac (n - 1)
  10. 10. Guards -- guards fac’ :: Int -> Int fac’ n | n == 0 = 1 | otherwise = n * fac’ (n - 1)
  11. 11. Pattern Matching -- pattern matching fac’’ :: Int -> Int fac’’ 0 = 1 fac’’ n = n * fac’’ (n - 1)
  12. 12. Exercises Template -- file: Main.hs module Main where import Prelude hiding (sum,length) sum :: [Int] -> Int ... length :: [a] -> Int ...
  13. 13. List Patterns [] xs (x:xs) (x:_) (_:xs) (_:_)
  14. 14. List Comprehensions > let xs = [2,4,7] > [ 2 * x | x <- xs ] [4,8,14] > [ even x | x <- xs ] [True,True,False] > [ 2 * x | x <- xs, even x, x > 3] [8] > [ x + y | (x,y) <- [(2,3),(2,1)] ] [5,3] > [ x + y | (x,y) <- [(2,3),(2,1)], x < y ] [5]
  15. 15. Summary computation types functions guards pattern matching list comprehensions
  16. 16. Coming Up programming with lists higher-order functions type classes algebraic types
  17. 17. List Functions (:) :: a -> [a] -> [a] tail, init :: [a] -> [a] (++) :: [a] -> [a] -> [a] replicate :: Int -> a -> [a] (!!) :: [a] -> Int -> [a] take, drop :: concat :: [[a]] -> [a] Int -> [a] -> [a] length :: [a] -> Int splitAt :: Int -> [a] -> ([a],[a]) head, last :: [a] -> a
  18. 18. More List Functions repeat :: a -> [a] and, or :: [Bool] -> Bool reverse :: [a] -> [a] sum, product :: zip :: [Int] -> Int [a] -> [b] -> [(a,b)] [Float] -> Float unzip :: [(a,b)] -> ([a],[b])
  19. 19. Programming with Lists Prelude> :load Sprite *Sprite> print glider .#. ..# ### [(),(),()] *Sprite> print (flipH glider) ### ..# .#. [(),(),()]
  20. 20. Sprite.hs flipH :: Picture -> Picture flipH pic = reverse pic flipV :: Picture -> Picture flipV pic = [ reverse line | line <- pic ]
  21. 21. Higher Order Functions functions as arguments functions as results or both
  22. 22. Higher Order Functions patterns of computation mapping: transforming elements filtering: selecting elements folding: combining elements
  23. 23. Mapping and Filtering map :: (a -> b) -> [a] -> [b] filter :: (a -> Bool) -> [a] -> [a] zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
  24. 24. Folding foldl :: (a -> b -> a) -> a -> [b] -> a foldl1 :: (a -> b -> a) -> [b] -> a foldr :: (a -> b -> b) -> b -> [a] -> b foldr1 :: (a -> a -> a) -> [a] -> a
  25. 25. Type Classes type class: a collection of of types over which certain functions are defined equality class Eq (==) :: (Eq a) => a -> a -> Bool (/=) :: (Eq a) => a -> a -> Bool
  26. 26. Declaring a Class class Visible a where toString :: a -> String size :: a -> Int class Eq a where (==), (/=) :: a -> a -> Bool x /= y = not (x == y) x == y = not (x /= y)
  27. 27. Defining an Instance instance Visible Char where toString ch = [ch] size _ = 1 instance Eq Bool where True == True = True False == False = True _ == _ = False
  28. 28. Derived Classes class Eq a => Ord a where (<), (<=), (>), (>=) :: a -> Bool max, min :: a -> a -> a compare :: a -> a -> Ordering
  29. 29. Built-In Classes Eq Ord Enum Show Read
  30. 30. Algebraic Types data Bool = False | True data Season = Spring | Summer | Autumn | Winter data Ordering = LT | EQ | GT
  31. 31. Product Types data People = Person Name Age type Name = String type Age = Int data Shape = Circle Double | Rectangle Float Float
  32. 32. Recursive Algebraic Types data Expr = Lit Int | Add Expr Expr | Sub Expr Expr data Tree a = Node (Tree a) (Tree a) | Leaf a
  33. 33. More? http://haskell.org/ G. Hutton: Programming in Haskell (Cambridge University Press) B. O’Sullivan, J. Goerzen, D. Stewart: Real World Haskell (O’Reilly)

×