Slide 1

Slide 1 text

Lessons learned at Haskell Private Vienna University of Technology J¨ urgen Cito, Michael Schr¨ oder Haskell Live Vienna University of Technology November 29th, 2013 1 / 12

Slide 2

Slide 2 text

A note on the code examples • The examples on these slides are for ghci • Hugs does not support let-expressions on the command prompt 2 / 12

Slide 3

Slide 3 text

Operations on Lists • Common task in FP is to work on lists • Ways we’ve seen more often: Recursion, List comprehension • Convenient ways to work with lists: • map • filter • zipWith 3 / 12

Slide 4

Slide 4 text

Working with lists – map Common task: Transforming elements in the list Source Element → Target Element map :: (a -> b) -> [a] -> [b] 4 / 12

Slide 5

Slide 5 text

Working with lists – map map :: (a -> b) -> [a] -> [b] Example: let words = ["my", "new", "string", "list"] let wordsWithLength word = (word, length word) map wordsWithLength words > [("my",2),("new",3),("string",6),("list",4)] 4 / 12

Slide 6

Slide 6 text

Anonymous Functions • Sometimes it’s more convenient to pass on a function without giving it a name. • Nameless functions, also called Lambda Expressions • from Lambda calculus: λx.x + 1 • in Haskell: \x -> x + 1 5 / 12

Slide 7

Slide 7 text

Anonymous Functions Example: let words = ["my", "new", "string", "list"] map (\word -> (word, length word)) words > [("my",2),("new",3),("string",6),("list",4)] 5 / 12

Slide 8

Slide 8 text

Anonymous Functions Example: let words = ["my", "new", "string", "list"] map (\word -> (word, length word)) words > [("my",2),("new",3),("string",6),("list",4)] -- ’it’ provides the last evaluated result in ghci map (\(a, b) -> (b, replicate b ’|’, a)) it > [(2,"||","my"),(3,"|||","new"),(6,"||||||","string"),(4,"||||","list")] 5 / 12

Slide 9

Slide 9 text

Working with lists – filter Retain elements that return true on given function, drop all others filter :: (a -> Bool) -> [a] -> [a] 6 / 12

Slide 10

Slide 10 text

Working with lists – filter filter :: (a -> Bool) -> [a] -> [a] Example: let wl = [("my",2),("new",3),("string",6),("list",4)] filter (\x -> snd x >= 4) wl > [("string",6),("list",4)] 6 / 12

Slide 11

Slide 11 text

Working with lists – filter filter :: (a -> Bool) -> [a] -> [a] Example: let wl = [("my",2),("new",3),("string",6),("list",4)] filter (\x -> snd x >= 4) wl > [("string",6),("list",4)] let count = [1,2,0] let candidateCount = [("Carla",1),("JD",2),("Turk",0)] filter (\x -> snd x == minimum count) candidateCount > [("Turk",0)] 6 / 12

Slide 12

Slide 12 text

Working with lists – zipWith Merge two lists into one with the help of a function zipWith :: (a -> b -> c) -> [a] -> [b] -> [c] 7 / 12

Slide 13

Slide 13 text

Working with lists – zipWith zipWith :: (a -> b -> c) -> [a] -> [b] -> [c] Example: let candidates = ["Carla", "JD", "Turk"] let count = [1,2,0] zipWith (\x y -> show y ++ ": " ++ x) candidates count > ["1: Carla", "2: JD", "0: Turk"] 7 / 12

Slide 14

Slide 14 text

$ Operator • $ Function application – right-associative, lowest precedence • ’Normal’ function application – left-associative, very high precedence • $ is a convenience function to avoid parentheses 8 / 12

Slide 15

Slide 15 text

$ Operator infixr 0 $ ($) :: (a -> b) -> a -> b f $ x = f x 8 / 12

Slide 16

Slide 16 text

$ Operator infixr 0 $ ($) :: (a -> b) -> a -> b f $ x = f x Example: sum (filter (> 10) (map (*2) [2..10])) > 80 8 / 12

Slide 17

Slide 17 text

$ Operator infixr 0 $ ($) :: (a -> b) -> a -> b f $ x = f x Example: sum (filter (> 10) (map (*2) [2..10])) > 80 sum $ filter (> 10) $ map (*2) [2..10] > 80 8 / 12

Slide 18

Slide 18 text

Function Composition with . Function composition is the act of pipelining the result of one function, to the input of another, creating an entirely new function.1 (.) :: (b -> c) -> (a -> b) -> a -> c f . g = \x -> f (g x) 9 / 12

Slide 19

Slide 19 text

Function Composition with . (.) :: (b -> c) -> (a -> b) -> a -> c f . g = \x -> f (g x) (reverse . sort) [2,8,7,10,1,9,5,3,4,6] > [10,9,8,7,6,5,4,3,2,1] 9 / 12

Slide 20

Slide 20 text

Function Composition with . (.) :: (b -> c) -> (a -> b) -> a -> c f . g = \x -> f (g x) (reverse . sort) [2,8,7,10,1,9,5,3,4,6] > [10,9,8,7,6,5,4,3,2,1] reverse . sort $ [2,8,7,10,1,9,5,3,4,6] > [10,9,8,7,6,5,4,3,2,1] 9 / 12

Slide 21

Slide 21 text

Function Composition with . (.) :: (b -> c) -> (a -> b) -> a -> c f . g = \x -> f (g x) (reverse . sort) [2,8,7,10,1,9,5,3,4,6] > [10,9,8,7,6,5,4,3,2,1] reverse . sort $ [2,8,7,10,1,9,5,3,4,6] > [10,9,8,7,6,5,4,3,2,1] rsort :: Ord a => [a] -> [a] rsort = reverse . sort rsort [2,8,7,10,1,9,5,3,4,6] > [10,9,8,7,6,5,4,3,2,1] 9 / 12

Slide 22

Slide 22 text

Function Composition with . (.) :: (b -> c) -> (a -> b) -> a -> c f . g = \x -> f (g x) (reverse . sort) [2,8,7,10,1,9,5,3,4,6] > [10,9,8,7,6,5,4,3,2,1] reverse . sort $ [2,8,7,10,1,9,5,3,4,6] > [10,9,8,7,6,5,4,3,2,1] rsort :: Ord a => [a] -> [a] rsort = reverse . sort rsort [2,8,7,10,1,9,5,3,4,6] > [10,9,8,7,6,5,4,3,2,1] remove p = filter (not . p) remove (\x -> mod x 2 == 0) [1..10] > [1,3,5,7,9] 9 / 12

Slide 23

Slide 23 text

@ – as-pattern Assigns matched pattern after ”@” to the symbol before ”@” so that this symbol can be used in the right-hand side expression1 1http: //zvon.org/other/haskell/Outputsyntax/As-patterns_reference.html 10 / 12

Slide 24

Slide 24 text

@ – as-pattern Example: rearrangeLostCandidates [] = [] rearrangeLostCandidates vote@(x:xs) | elem x lostCandidates = xs++[x] | otherwise = vote 10 / 12

Slide 25

Slide 25 text

@ – as-pattern Example: rearrangeLostCandidates [] = [] rearrangeLostCandidates vote@(x:xs) | elem x lostCandidates = xs++[x] | otherwise = vote delete :: SozNr -> Tree -> Tree delete _ Nil = Nil delete s@(Sznr snr) (Node n@(_, _, Sznr nr) left right) | snr < nr = Node n (delete s left) right | snr > nr = Node n left (delete s right) 10 / 12

Slide 26

Slide 26 text

Higher Order Functions A higher-order function is a function that takes other functions as arguments or returns a function as result.1 ⇒ map, filter, foldl, zipWith, ... we’ve been passing functions everywhere already 1http://www.haskell.org/haskellwiki/Higher_order_function 11 / 12

Slide 27

Slide 27 text

Higher Order Functions Example: :t quEndergebnis > quEndergebnis :: Int -> [String] -> [[Int]] -> String :t endergebnis > endergebnis :: [String] -> [[Int]] -> String 11 / 12

Slide 28

Slide 28 text

Higher Order Functions Example: :t quEndergebnis > quEndergebnis :: Int -> [String] -> [[Int]] -> String :t endergebnis > endergebnis :: [String] -> [[Int]] -> String :t (quEndergebnis 50) > (quEndergebnis 50) :: [String] -> [[Int]] -> String 11 / 12

Slide 29

Slide 29 text

Higher Order Functions Example: :t quEndergebnis > quEndergebnis :: Int -> [String] -> [[Int]] -> String :t endergebnis > endergebnis :: [String] -> [[Int]] -> String :t (quEndergebnis 50) > (quEndergebnis 50) :: [String] -> [[Int]] -> String endergebnis = quEndergebnis 50 11 / 12

Slide 30

Slide 30 text

Thanks! Questions? 12 / 12