2. Why I started to learn functional
programming
1. Interesting and useful concepts for general software
development
2. For fun
Structure and Interpretation of Computer Programs (SICP)
http://en.wikipedia.org/wiki/
Structure_and_Interpretation_of_Computer_Programs
3. 1. Interesting and useful concepts for general software
development
2. For fun
Structure and Interpretation of Computer Programs (SICP)
http://en.wikipedia.org/wiki/
Structure_and_Interpretation_of_Computer_Programs
(3. Hipster)
Why I started to learn functional
programming
4. Goal of this talk
1. Show some functional programming concepts which
can be used in your day to day programming in a
non-functional programming language
2. Get you interested in functional programming
5. What is functional programming?
Declarative vs Imperative
Functional Programming is a kind of declarative programming
Imperative
“Describes computation in terms of statements that change a program state (...) describing
how a program operates.”
http://en.wikipedia.org/wiki/Imperative_programming
Declarative
“A style of building the structure and elements of computer programs, that expresses the logic
of a computation without describing its control flow. (...) describing what the program should
accomplish (...), rather than describing how”
http://en.wikipedia.org/wiki/Declarative_programming
8. Functional Programming concepts:
Recursion
● Is a method where the solution to a
problem depends on solutions to smaller
instances of the same problem
● Recursion, see Recursion
● Recursion is heavily used in functional
programming as it is the canonical and
often the only way to iterate
Image from: http://learnyouahaskell.com/
11. Functional Programming concepts:
Higher order functions
● Higher order function example
● accumulate from http://mitpress.mit.edu/sicp/full-text/book/
book-Z-H-15.html#%_sec_2.2.3
13. Functional Programming concepts:
Referential transparency
● A program (or function) that has the same effects and
output on the same input
● Allows the programmer and the compiler to reason
about program behavior
● Try to make all functions referential transparent and
keep the side effects in one place
14. Functional Programming concepts:
Referential transparency
Useful
Useless
Unsafe
(side effects everywhere)
Safe
(Limited side effects)
• C
• Java
• … “normal” languages
• Haskell
• (probably other pure funcional laguages)
https://www.youtube.com/watch?v=iSmkqocn0oQ
Future
15. Functional Programming concepts:
Referential transparency
● A pure function can always be replaced by its result.
● There’s no difference between Math.cos(Math::PI)
and -1.0
● A function which calls an impure function is impure as
well. Impurity is contagious
http://www.sitepoint.com/functional-programming-pure-
functions/
18. Functional Programming concepts:
Referential transparency
Example from: http://www.sitepoint.com/functional-programming-pure-functions
Same function.
Only if it is a pure
function
19. Functional Programming concepts:
Lazy evaluation
● Pure functions can be performed at any time and
still yield the same result.
● Defer the computation of values until they are
needed
● Example
20. Which of this concepts I use in ruby and
how
● Higher order functions
o map
o reduce
o select
o find
● Pure functions (referential transparency)
25. Current stand = Lets take this functions as “curried”
or partially applied with the objects date.
self.date not modifiable
26. Conclusions
● Learn new approaches to solve problems
● Try functional programing!!
● It will help you to create better code.
A language that doesn’t affect the way you think about programming, is not worth knowing.
— Alan Perlis
27. Resources
● Structure and Interpretation of Computer Programs (SICP) http://
mitpress.mit.edu/sicp/full-text/book/book-Z-H-38.html#
%_index_start
● Programming with Nothing http://codon.com/programming-with-
nothing
28. References
● Haskell wiki https://wiki.haskell.org/Functional_programming
● Composing programs (Higher order functions) http://
www.composingprograms.com/pages/16-higher-order-
functions.html
● Learn You a Haskell for Great Good! http://learnyouahaskell.com
● http://www.sitepoint.com/functional-programming-pure-functions/