Upcoming SlideShare
×

Functional Programming #FTW

2,757 views

Published on

An introduction to the basic concepts on functional programming, explaining why it is a hot topic for some years now, what it is and some suggestions of functional languages to be learned.

Published in: Technology, Education
5 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total views
2,757
On SlideShare
0
From Embeds
0
Number of Embeds
30
Actions
Shares
0
47
0
Likes
5
Embeds 0
No embeds

No notes for slide

Functional Programming #FTW

2. 2. Why FP is a hot topic? • Multicore CPUs • VM based languages • Great and easy to write DSLs • Parsers • Hardware design • Expressiveness
3. 3. What is it? • Different programming paradigm • OO • Logic • Procedural • Functions are the main element in the language
4. 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. 5. Origin Alonzo Church developed Lambda Calculus as part of his investigations on Math foundations on 1936.
6. 6. Lambda Calculus • Variables • Expressions (e e ) 1 2 • Lambda abstractions (λx. e)
7. 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. 8. Basic concepts • Pureness • Pattern Matching • Lazyness • High Order Functions • Curriﬁcation (aka Partial Application)
9. 9. 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”
10. 10. Imperative style function sum(elems: list of int) returns int { int total = 0; for elem in elems { total = total + elem; } return total; }
11. 11. Imperative style function sum(elems: list of int) returns int { int total = 0; for elem in elems { total = total + elem; } return total; } Computation method is variable assignment
12. 12. Functional style Pattern matching is cool! Deﬁnition: sum [] = 0 sum elem:rest = elem + sum rest Application: sum [1,2,3,10]
13. 13. Functional style Deﬁnition: sum [] = 0 sum elem:rest = elem + sum rest Application: sum [1,2,3,10] Computation method is function application
14. 14. Lazyness • aka “call by need” • Expressions can be evaluated when necessary • Allows the use of inﬁnite lists
15. 15. Lazyness (I) Deﬁnition: even_numbers :: [Int] even_numbers = filter even [1..] Application: take 5 even_numbers
16. 16. Lazyness (II) fibs :: [Int] fibs = 0 : 1 : zipWith (+) fibs (tail fibs) From: http://en.wikipedia.org/wiki/Lazy_evaluation
17. 17. High Order Functions Functions which at least: • Receive functions as parameters • Return functions (aka curried functions)
18. 18. High Order Functions (I) map :: (a -> b) -> [a] -> [b] map f [] = [] map f (x:xs) = f x : map f xs
19. 19. Curriﬁcation sum :: Int -> Int -> Int sum x y = x + y inc :: Int -> Int inc = sum 1
20. 20. Where to go now? Suggestions: • Haskell • LISP • Scheme • Common LISP (CL) • Clojure • Erlang • Scala • F#
21. 21. Haskell • Pure n’ lazy • Static typed • Type classes • Monads • Lots of research & dev.