Your browser doesn't support the features required by impress.js, so you are presented with a simplified version of this presentation.
For the best experience please use the latest Chrome, Safari or Firefox browser.
Sample Code:
As you can see this function accepts two integers and returns one this is not a curried function.
add :: (Integer, Integer) -> Integer
This function takes one integer, followed by a function that takes an integer, and finall returns an integer, this is an example of currying.
add :: Integer -> Integer -> Integer
Currying is often confused with partial-evaluation, however the key difference is with currying you are not infact creating a function accepts less than the repuired number of arguments, you are actually creating a chain of functions that each accept one function.
A way to call a function that takes multiple arguments while providing a fewer arguments than required
Yields a new function that accepts the remaining arguments.
Regular partial application done by most languages simply binds the given arguments using closures (we'll explain later)
True 'partial evaluation' will actually invoke parts of this function
This is a basic function that accepts 3 parameters and returns the average. This is a partial application.
average x y z = (x + y + z) `div` 3
Source
Here we are partially applying the function we created earlier.
average x y z = (x + y + z) `div` 3
first = average 1
second = first 2
third = second 3
Source
Given an operation whose arguments completely determine its output, repeated calls to that operation with the same arguments will always produce the same output.
Memoization is a programming technique that takes advantage of this by recording the result of each invocation of an operation against the given arguments, and, when given the same arguments again, returns the stored result instead of invoking the operation again.
The concept of closure is the concept of letting a function have access to things that are scoped around it, and tying the lifetime of those things to the lifetime of the function.
function findAverage(x, y, cb) {
function findSum() {
return x + y;
}
var result = fundSum() / 2;
cb(result);
}
Here is a very basic example of memoization in Python that wraps a function in a object to memoize it.
def memoize(f):
memo = {}
def delegate(*args):
if not args in memo:
memo[args] = f(*args)
return memo[args]
return delegate
def buildURI(a, b, c, d):
return a + "://" + b + ":" + `c` + "/" + d
buildURI = memoize(buildURI)
Source
Here is a proper example in Haskell
times a b = a * b
timesM f' x y = do
return $ times x y
type StateMap a b = State (Map a b) b
memoizeM t x y = evalState (f x y) Map.empty where
g x y = do
y <- t f x y
m <- get
put $ Map.insert x y m
newM <- get
return y
f x y = get >>= \m -> maybe (g x y) return (Map.lookup x m)
val x y = memoizeM timesM x y
Source
Lambda expressions are functions not bound to an identifier, a common example of this is in-line functions.
Something we have done many times in javascript is a perfect example, callbacks:
function findSum(x, y, cb) {
var result = x + y;
cb(result);
}
findSum(1, 2, function(x) {
window.alert(x);
});
Source
Again in a functional language, with no side-effects we demonstrate a very similar piece of code:
findSum :: Integer -> Integer -> (Integer -> Integer) -> Integer
findSum x y f = (f(x+y))
main =
print(findSum 2 3 (\x -> x * 2))
Source
Recursion is not a concept unique to functional programming, however the important note is that in functional programming their are no loops.
countToTen x =
if x < 10 then
countToTen(x + 1)
else
x
String salutation = // pure functional expression
customers.filter((Customer c) -> { c.lastName == "Smith"; }) // lambda
.transform((Customer c) -> { return capitalize(c.firstName); }) // map/reduce paradigm
.reduce("", (String name, String rest) -> { rest.append(" ").append(name); })
Promise promise = executor.submit({ /* do something expensive */ });
promise.andThen(println); // now output the result (first class function; not pure functional!)
Use a spacebar or arrow keys to navigate