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
0 Comments
5 Likes
Statistics
Notes
  • Be the first to comment

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

No notes for slide

Functional Programming #FTW

  1. 1. Functional Programming #FTW Adriano Bonat adrianob@gmail.com @tanob
  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 defined in terms of other functions, which in turn are defined 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 • Currification (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! Definition: sum [] = 0 sum elem:rest = elem + sum rest Application: sum [1,2,3,10]
  13. 13. Functional style Definition: 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 infinite lists
  15. 15. Lazyness (I) Definition: 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. Currification 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.
  22. 22. Haskell (I) Freely available online: http://book.realworldhaskell.org/
  23. 23. LISP • First functional language • Lots of flavors • Deal with parenthesis! • Dynamic typed • Homoiconic
  24. 24. Scheme Freely available online: http://mitpress.mit.edu/sicp/
  25. 25. Clojure • Runs on JVM • Concurrency
  26. 26. Erlang • Fault tolerant systems • Concurrency • Super easy! • Dynamic typed
  27. 27. Scala • Runs on JVM • Several nice features • Looks complex • Static typed
  28. 28. F# • Runs on .NET • Result of MS’s investment on FP • Simon Peyton Jones (Haskell contrib) • Don Syme (F# creator) • Erik Meijer (FP advocator, LINQ creator)
  29. 29. F# (I) Pragmatic F# In Action Josh Graham and Amanda Laucher http://www.infoq.com/presentations/Pragmatic-F-Sharp-in-Action
  30. 30. 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
  31. 31. Functional Programming #FTW Adriano Bonat adrianob@gmail.com @tanob

×