Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our User Agreement and Privacy Policy.

Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our Privacy Policy and User Agreement for details.

Like this presentation? Why not share!

- Proposal writing refresher - handou... by David Thomas 434 views
- Refresher by Loek Hopstaken 1183 views
- Nonprofit 2015 Refresher Jeanine Gu... by ConnectVA 439 views
- Caseware refresher slides by Matthew Green 1858 views
- Machine Learning Preliminaries and ... by butest 600 views
- 2016 Hazwoper 8 Refresher - Gauthier by Adam Gauthier, AS... 40 views

418 views

366 views

366 views

Published on

Refresher Slides

No Downloads

Total views

418

On SlideShare

0

From Embeds

0

Number of Embeds

0

Shares

0

Downloads

9

Comments

0

Likes

1

No embeds

No notes for slide

- 1. Refresher Sebastian Rettig“Work on Haskell began in 1987 when aa committee of “Work on Haskell began in 1987 when committee ofresearchers got together to design aa kick-asslanguage.” ([1]) researchers got together to design kick-ass language.” ([1])
- 2. Imperative Programming● Variables (value placeholder) – Value can change during program flow● Modules, Classes, Functions, Procedures● Control Flow Structures (IF THEN ELSE, Loops (WHILE), Goto)
- 3. Functional Programming● No Variables● Functions only, eventually stored in Modules – Behavior do not change, once defined – → Function called with same parameter calculates always the same result● Function definitions (Match Cases)● Recursion
- 4. Haskell Refresher Quiz● Whats the red one ?●maxList :: (Ord a) => [a] -> amaxList [] = error “empty”maxList [x] = xmaxList (x:xs) | x > maxTail = x | otherwise = maxTail where maxTail = maxList xs
- 5. Haskell Refresher Quiz● Whats the red one ?●maxList :: (Ord a) => [a] -> a FunctionmaxList [] = error “empty”maxList [x] = xmaxList (x:xs) | x > maxTail = x | otherwise = maxTail where maxTail = maxList xs
- 6. Haskell Refresher Quiz● Whats the red one ?●maxList :: (Ord a) => [a] -> amaxList [] = error “empty”maxList [x] = xmaxList (x:xs) | x > maxTail = x | otherwise = maxTail where maxTail = maxList xs
- 7. Haskell Refresher Quiz● Whats the red one ?●maxList :: (Ord a) => [a] -> a Function HeadermaxList [] = error “empty”maxList [x] = xmaxList (x:xs) | x > maxTail = x | otherwise = maxTail where maxTail = maxList xs
- 8. Haskell Refresher Quiz● Whats the red one ? maxList :: [Int] -> Int maxList [] = error “empty” maxList [x] = x maxList (x:xs) | x > maxTail = x | otherwise = maxTail where maxTail = maxList xs
- 9. Haskell Refresher Quiz● Whats the red one ?● maxList :: [Int] -> Int● maxList [] = error “empty” maxList [x] = x maxList (x:xs) | x > maxTail = x Function Body | otherwise = maxTail where maxTail = maxList xs
- 10. Haskell Functions● function contains header and body● header consists of type definition: <funcname> :: <param> [ -> <param_n>] -> <result>● body consists of pattern rules and calculation <funcname> <paramalias_1> [<paramalias_n>] = {calc}● Example (2 params if type [Int] & Int, result Bool): isHead :: [Int] -> Int -> Bool isHead xs i = i==head xs
- 11. Haskell Refresher Quiz● Whats the red one ?● maxList :: [Int] -> Int maxList [] = error “empty” maxList [x] = x maxList (x:xs) | x > maxTail = x | otherwise = maxTail where maxTail = maxList xs
- 12. Haskell Refresher Quiz● Whats the red one ?● maxList :: [Int] -> Int Type maxList [] = error “empty” maxList [x] = x maxList (x:xs) | x > maxTail = x | otherwise = maxTail where maxTail = maxList xs
- 13. Types in Haskell (1)Starts with uppercase first character● Int bounded from -2147483648 to 2147483647● Integer unbounded (for big numbers) but slower than Int● Float floating point (single precision)● Double floating point (double precision)● Bool boolean● Char character● String list of Char (String == [Char])
- 14. Types in Haskell (2)● Lists: must be homogenous (entries from the same type) – [] empty List – [Int] List of Int – But e.g. [Int, Bool] not allowed (not homogenous)!● Tuples: can contain different types BUT have fixed length – () empty tuple (has only 1 value) – (Int, Bool) tuple of a pair Int, Bool – (Int, Bool, Bool) tuple of Int, Bool, Bool
- 15. Lists & List Comprehension● Example: double entries in list doubleList [] = [] doubleList (x:xs) = x:x:doubleList xs – e.g. [1,2,3] → x=1; xs=[2,3]● Multiplication of a list of values: Hugs> product [1..5] 120● or in a function: fac n = product [1..n]
- 16. Haskell Refresher Quiz● Whats the red one ?●maxList :: [Int] -> IntmaxList [] = error “empty”maxList [x] = xmaxList (x:xs) | x > maxTail = x | otherwise = maxTail where maxTail = maxList xs
- 17. Haskell Refresher Quiz● Whats the red one ?●maxList :: [Int] -> IntmaxList [] =maxList [x] = x error “empty” PatternmaxList (x:xs) Matching | x > maxTail = x | otherwise = maxTail where maxTail = maxList xs
- 18. Haskell Refresher Quiz● Whats the red one ?● maxList :: [Int] -> Int maxList [] = error “empty” maxList [x] = x maxList (x:xs) | x > maxTail = x | otherwise = maxTail where maxTail = maxList xs
- 19. Haskell Refresher Quiz● Whats the red one ?● maxList :: [Int] -> Int maxList [] = error “empty” maxList [x] = x maxList (x:xs) | x > maxTail = x | otherwise = maxTail Guards where maxTail = maxList xs
- 20. Haskell Refresher Quiz● Whats the red one ?● maxList :: [Int] -> Int maxList [] = error “empty” maxList [x] = x maxList (x:xs) | x > maxTail = x | otherwise = maxTail where maxTail = maxList xs
- 21. Haskell Refresher Quiz● Whats the red one ?● maxList :: [Int] -> Int maxList [] = error “empty” maxList [x] = x maxList (x:xs) | x > maxTail = x | otherwise = maxTail Additional where maxTail = maxList xs where clause
- 22. Pattern Matching (1)● create matching rules: fac 0 = 1 fac n = n * fac (n-1)● use wildcards to ignore parameters in pattern: take 0 _ = [] take _ [] = [] take n (x:xs) = [x] ++ take (n-1) xs
- 23. Pattern Matching (2)● use Guards to separate a matching case deeper: myFilter _ [] = [] myFilter f (x:xs) | f==x = x:myFilter g xs | otherwise = myFilter f xs where g = 2*f – like an IF THEN ELSE – Guard Condition evaluate to Bool (True/False)● eventually define values with where-clause myFilter 2 [1,2,3,4,5,6] results to [2,4]
- 24. Recursion● Recursion vs. Final Recursion:countX :: Int -> [Int] -> Int ● Hugs> countX 3 [1,4,3,5,3]countX x [] = 0 2countX x (y:ys) | x==y = 1 + countX x ys | otherwise = countX x ys countXFinal :: Int -> [Int] -> Int -> Int countXFinal x [] accu = accu countXFinal x (y:ys) accu | x==y = countXFinal x ys accu+1 | otherwise = countXFinal x ys accu● use accumulator to reduce stack usage● Hugs> countXFinal 3 [1,4,3,5,3] 0 2
- 25. Haskell Refresher Quiz ● Whats the red one ? ●maxList :: (Ord a) => [a] -> amaxList [] = error “empty”maxList [x] = xmaxList (x:xs) | x > maxTail = x | otherwise = maxTail where maxTail = maxList xs
- 26. Haskell Refresher Quiz ● Whats the red one ? ●maxList :: (Ord a) => [a] -> a TypeclassmaxList [] = error “empty”maxList [x] = xmaxList (x:xs) | x > maxTail = x | otherwise = maxTail where maxTail = maxList xs
- 27. Haskell Refresher Quiz ● Whats the red one ? ●maxList :: (Ord a ) => [a] -> amaxList [] = error “empty”maxList [x] = xmaxList (x:xs) | x > maxTail = x | otherwise = maxTail where maxTail = maxList xs
- 28. Haskell Refresher Quiz ● Whats the red one ? ●maxList :: (Ord a ) => [a] -> a Type variablemaxList [] = error “empty”maxList [x] = xmaxList (x:xs) | x > maxTail = x | otherwise = maxTail where maxTail = maxList xs
- 29. Type Polymorphism (1)● Statically typed, but Compiler can read type from context (type inference)● → no need to set type explicitly● → makes function more generic for different kinds of types (type polymorphism) – Why should I use quicksort :: [Int] -> [Int] – even if I also want to sort character? Hugs> quicksort [f,a,d,b] "abdf"
- 30. Type Polymorphism (2)● the header of our previous implementations could be fact :: Int -> Int maxList :: [Int] -> Int● but is only limited to Int, but maxList could also handle Char● → why not make it generic? maxList :: [a] -> a● but what happens, if the corresponding Type is not comparable or cannot be ordered?
- 31. Type Polymorphism (3)● Solution: use Typeclasses maxList :: (Ord a) => [a] -> a● then we can be sure to use (<,<=, ==, /=, >=, >)● function header can contain multiple typeclasses maxList :: (Ord a, Eq b) => [a] -> [b] -> a● In Haskell-Interpreter: to list the function header :t <function_name>
- 32. Typeclasses (1)● define properties of the types● like an interface● Typeclasses: – Eq can be compared – Ord can be ordered (>, <, >=, <=) (extending Eq) – Show can be shown as string – Read opposite of Show – Enum sequentially ordered types (can be enumerated and usable in List-Ranges [a..e])
- 33. Typeclasses (2)● Typeclasses: – Bounded upper/lower bound (minBound, maxBound) – Num types behave like numbers (must already be Show, Eq) – Integral contains only integrals (subclass of Num) – Floating corresponding real numbers (subclass of Num)● if all Types of tuple are in same Typeclass → Tuple also in Typeclass
- 34. Haskell Refresher Quiz● Which will terminate? (1) head [1..] (2) last [1..] (3) tail [1,2,3,4,5,6] (4) init (take 5 (cycle [1..]))
- 35. Haskell Refresher Quiz● Which will terminate? (1) head [1..] (2) last [1..] (3) tail [1,2,3,4,5,6] (4) init (take 5 (cycle [1..]))● (1), (3) and (4) because of Lazy Evaluation
- 36. Lazy Evaluation● Function execution only if result is needed● → Program = series of data-transformations● Example: A(B(C(x))) – If A needs result from B → call B – If B needs result from C → call C
- 37. Haskell Refresher Quiz● Which one is faster? (1) head (take 5 [1,2,3,4,5,6]) (2) head (take 5 [1..10]) (3) head (take 5 [1..]) (4) head (take 5 (cycle [1..10]))
- 38. Haskell Refresher Quiz● Which one is faster? (1) head (take 5 [1,2,3,4,5,6]) (2) head (take 5 [1..10]) (3) head (take 5 [1..]) (4) head (take 5 (cycle [1..10]))● Everyone is equal because of Lazy Loading
- 39. Sources[1] Haskell-Tutorial: Learn you a Haskell (http://learnyouahaskell.com/, 2012/03/15)[2] The Hugs User-Manual ( http://cvs.haskell.org/Hugs/pages/hugsman/index.html, 2012/03/15)[3] The Haskellwiki (http://www.haskell.org/haskellwiki, 2012/03/15)

No public clipboards found for this slide

×
### Save the most important slides with Clipping

Clipping is a handy way to collect and organize the most important slides from a presentation. You can keep your great finds in clipboards organized around topics.

Be the first to comment