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!

- Mantendo seu ambiente de CI na nuvem by Adriano Bonat 185 views
- Funcionamento e otimização da Gar... by Adriano Bonat 754 views
- Functional Programming #FTW by Adriano Bonat 2755 views
- Programando arduino com javascript by Desiree Santos 337 views
- How to Fix the Internet by LinkedIn Pulse 26250 views
- 32 Ways a Digital Marketing Consult... by Barry Feldman 62303 views

680 views

Published on

License: CC Attribution License

No Downloads

Total views

680

On SlideShare

0

From Embeds

0

Number of Embeds

1

Shares

0

Downloads

14

Comments

0

Likes

1

No embeds

No notes for slide

- 1. Functional Programming with Haskell Adriano Bonat abonat@thoughtworks.com @tanob
- 2. Why FP? • Source of new ideas • Expressiveness • Multi-core CPUs • Different paradigm New ideas: Garbage collection (LISP) Type inference (simply typed lambda calculus) Generics Type classes Expressiveness: DSLs
- 3. What is it? • Different programming paradigm • OO • Logic • Procedural • Functions are the main element in the language
- 4. Function applications “Functional programming is so called because a program consists entirely of functions. [...] Typically the main function is deﬁned in terms of other functions, which in turn are deﬁned in terms of still more functions, until at the bottom level the functions are language primitives.” John Hughes, 1989 -Why functional programming matters
- 5. Origin Alonzo Church developed Lambda Calculus as part of his investigations on Math foundations on 1936.
- 6. Lambda Calculus • Variables • Expressions (e1 e2) • Lambda abstractions (λx. e)
- 7. Lambda Calculus (I) • true = λxy. x • false = λxy. y • NOT a = (a)(false)(true) • a AND b = (a)(b)(false) • a OR b = (a)(true)(b) • a XOR b = (a)((b)(false)(true))(b)
- 8. Haskell • Academic origin • Named in honor of Haskell Curry • Deﬁned by a committee • First version released on 98 (Haskell 98)
- 9. Features • Pureness • Type Inference • Algebraic datatypes (ADTs) • Pattern Matching • Lazyness • High Order Functions • Curriﬁcation (aka Partial Application) • Type Classes • Monads
- 10. Pureness • No side-effects • A function call can have no effect other than to compute its result • Expressions can be evaluated at any time • Programs are “referentially transparent” Good for: * reasoning * compiler optimization * concurrency
- 11. Type Inference Let’s see the types for these declarations: four = 4 add x y = x + y emphasize x = x ++ “!”
- 12. Algebraic datatypes Enumeration: data Season = Summer | Winter | Autumn | Spring Product: data Pair = Pair Int Int Sum: data Shape = Circle Float | Rect Float Float Polymorﬁc & Recursive: data Tree a = Leaf a | Node (Tree a) (Tree a)
- 13. Algebraic datatypes (I) data Maybe a = Nothing | Just a data Either a b = Left a | Right b
- 14. Pattern Matching Deﬁnition: sum [] = 0 sum (elem:rest) = elem + sum rest Application: sum [1,2,3,10]
- 15. Pattern Matching (I) area (Circle rad) = pi * rad ^ 2 area (Rect width height) = width * height ﬁrst (Pair value _) = value
- 16. High Order Functions Functions which at least: • Receive functions as parameters • Return functions (aka curried functions)
- 17. High Order Functions (I) map :: (a -> b) -> [a] -> [b] map f [] = [] map f (x:xs) = f x : map f xs
- 18. Curriﬁcation add :: Int -> Int -> Int add x y = x + y inc :: Int -> Int inc = add 1
- 19. Lazyness • aka “call by need” • Expressions can be evaluated when necessary • Allows the use of inﬁnite lists Being pure helps here
- 20. Lazyness (I) Deﬁnition: even_numbers :: [Int] even_numbers = filter even [1..] Application: take 5 even_numbers
- 21. Lazyness (II) fibs :: [Int] fibs = 0 : 1 : zipWith (+) fibs (tail fibs) From: http://en.wikipedia.org/wiki/Lazy_evaluation
- 22. Type Classes • Created to solve the problem with numeric operator overload and equality testing • Some type classes deﬁned by Haskell 98: • Eq • Read/Show
- 23. Type Classes (I) class Eq a where (==), (/=) :: a -> a -> Bool x == y = not (x /= y) x /= y = not (x == y) You can define what is called a “minimal implementation”.
- 24. Type Classes (II) data User = User { name :: String } instance Eq User where user1 == user2 = name user1 == name user2 instance Show User where show user = name user
- 25. Automatic Derivation data Season = Summer | Winter | Autumn | Spring deriving (Show, Eq) show Summer > “Summer” Summer /=Winter > True
- 26. Monads • Adds to the type system a way to describe actions • The actions will happen in a certain order
- 27. Monads • Common monads: • IO • State • Reader • Maybe
- 28. Monads thing1 >>= x -> func1 x >>= y -> thing2 >>= _ -> func2 y >>= z -> return z do x <- thing1 y <- func1 x thing2 z <- func2 y return z sugar no-sugar
- 29. Monads class Monad m where (>>=) :: m a -> (a -> m b) -> m b (>>) :: m a -> m b -> m b return :: a -> m a “return” is a bad name, it actually injects a value into the monadic type.
- 30. Logger Monad type Log = [String] data Logger resultType = Logger (resultType, Log) deriving Show record x = Logger ((), [x]) instance Monad Logger where return value = Logger (value, []) prevLogger >>= nextAction = let Logger (prevResult, prevLog) = prevLogger Logger (newResult, newLog) = nextAction prevResult in Logger (newResult, prevLog ++ newLog)
- 31. Testing? • Go read about QuickCheck!
- 32. Want to learn more? Freely available online: http://book.realworldhaskell.org/
- 33. Your Knowledge Portfolio "Learn at least one new language every year. [...] Different languages solve the same problems in different ways. By learning several different approaches, you can help broaden your thinking and avoid getting stuck in a rut." The Pragmatic Programmer
- 34. Functional Programming with Haskell Adriano Bonat abonat@thoughtworks.com @tanob

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