# 02. haskell motivation

Post on 11-May-2015

352 views

Embed Size (px)

DESCRIPTION

Haskell Meetup Session 2TRANSCRIPT

- 1.Motivation Sebastian RettigRecursion is important to Haskell because unlike imperative Recursion is important to Haskell because unlike imperativelanguages, you do computations in Haskell by declaring languages, you do computations in Haskell by declaringwhat something is instead of declaring how you get it. ([1]) what something is instead of declaring how you get it. ([1])

2. Functional Programming No Variables Functions only, eventually stored in Modules Behavior do not change, once defined Function called with same parametercalculates always the same result Function definitions (Match Cases) Recursion (Memory) 3. Haskell Features Pure Functional Programming Language Lazy Evaluation Pattern Matching and Guards List Comprehension Type Polymorphism 4. When you start... You want to do big projects with Haskell? Then start simple, because: a function is Simple and...Abstraction- SimpleLevelSimpleSimpleBIG SimpleSimpleSimpleProjectSimpleSimpleSimpleSimple 5. Lets start simple You need a Framework to start with Haskell? then write one, if you really think you need one :) but be aware of Haskell == simple Framework == should also be simple a simple function: just ignore the function header at the beginningcountNum 1 = OnecountNum 2 = TwocountNum x = toooo difficult to count... 6. How to implement the Factorial? Remember:Recursion is important to Haskell because unlike imperativelanguages, you do computations in Haskell by declaring whatsomething is instead of declaring how you get it. ([1]) Okay, then look at the definition: Imperative definition Recursive definition 7. Lets look at the imperative way... Imperative Recursivefunction factorial(n) {int result = 1;if (n == 0)return result;}for (int i=1; i max) {max = list[i];}}return max;} 11. and translate to the functional way Imperative Recursivefunction max(array list) {maxList [] = error emptyif (empty(list)) {maxList [x] = xthrow new Exception();maxList (x:xs)} | x > maxTail = xmax = list[0] | otherwise = maxTailfor (int i=0; i max) { or simpler:max = list[i];maxList [] = error empty} maxList [x] = x} maxList (x:xs) =return max;max x (maxList xs)} and in comparison with thedefinition: 12. Types in Haskell (1)Starts with uppercase first characterInt bounded from -2147483648 to 2147483647Integer unbounded (for big numbers) but slower than IntFloat floating point (single precision)Double floating point (double precision)Bool booleanChar characterString list of Char (String == [Char]) 13. 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 14. List-Functions head returns first element of list head [1,2,3] returns 1 tail returns list without first element tail [1,2,3] returns [2,3] init returns list without last element init [1,2,3] returns [1,2] last returns last element of list last [1,2,3] returns 3 15. Tuple-Functions fst returns first element of a pair fst (1, one) returns 1 only usable on Tuples of Pairs!!! fst (1,one,True) throws Exception!!! snd returns second element of a pair snd (1, one) returns one only usable on Tuples of Pairs!!! snd (1,one,True) throws Exception!!! zip creates a list of tuples from two lists zip [1,2,3] [one,two,three] returns [(1,one), (2,two),(3,three)] 16. Type Polymorphism (1) Statically typed, but Compiler can read type fromcontext (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" 17. Type Polymorphism (2) the header of our previous implementations could befact :: Int -> IntmaxList :: [Int] -> Int but is only limited to Int, but maxList could alsohandle Char why not make it generic?maxList :: [a] -> a but what happens, if the corresponding Type is notcomparable or cannot be ordered? 18. Type Polymorphism (3) Solution: use TypeclassesmaxList :: (Ord a) => [a] -> a then we can be sure to use () function header can contain multiple typeclassesmaxList :: (Ord a, Eq b) => [a] -> [b] -> a In Haskell-Interpreter: to list the function header:t 19. Typeclasses (1) define properties of the types like an interface Typeclasses: Eq can be compared Ord can be ordered (>, =, b = a| otherwise = bwhere temp = cycle LOL 22. 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)

Recommended