Upload
adriangzz2112
View
58
Download
1
Embed Size (px)
Citation preview
Why I started to learn functional programming1. 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
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
Goal of this talk1. 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
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
ExamplesImperative Functional
Imperative: http://www.siafoo.net/snippet/306 Functional: http://en.literateprograms.org/Quicksort_%28Haskell%29
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/
Functional Programming concepts: Higher order functions
● Functions that take other functions as their arguments
● Functions that returns other functions
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
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
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
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/
Functional Programming concepts: Referential transparency
Example from: http://valve.github.io/blog/2014/07/04/from-object-to-functional-immutability/
Referential opaque Referential transparent
Functional Programming concepts: Referential transparency
● Easy to test (it does not depend on anything but its input)
● Easy maintenance ● Concurrency/parallelization
Functional Programming concepts: Referential transparency
Example from: http://www.sitepoint.com/functional-programming-pure-functions/
Same function. Only if it is a pure function
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
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)
Current stand = Lets take this functions as “curried” or partially applied with the objects date. self.date not modifiable
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
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
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/