On fuctional programming, high order functions, ML
-
Upload
toretto460-di-maulo -
Category
Technology
-
view
366 -
download
6
description
Transcript of On fuctional programming, high order functions, ML
WhyFunctional Programming
Matters
“Well-structured software is easy to write to and to debug, and provides a collection of modules that can reused to reduce future programming costs.”John Hughes - The university, Glasgow
WhyFunctional Programming
Matters
?
What is a function
The basic, and not very enlightening definition is this: “in a functional language, functions are first-class citizens.”
What is a function
The mean is that the function takes a function as one of its arguments.
let double x = x * 2 inList.map double [ 1; 2; 3 ];;- : int list = [2; 4; 6]
What is a functional program
FP won't make your word processing program faster or better. But there are domains where it is highly useful, and in particular FP looks like the paradigm of choice for unlocking the power of multicore processors.
What is a functional program
A functional program contains no assignment statements, so the variables once defined never change their value.The property to change the variable’s value is called “side effect”...
A functional program has no side effect.
A function call can have no effect other than to compute its result, so the function can be evaluated at any time because the no side-effect.
A function call can have no effect other than to compute its result, so the function can be evaluated at any time because the no side-effect.
A function call can have no effect other than to compute its result, so the function can be evaluated at any time because the no side-effect.
multi core calculus
A function
val double = fn x:int =>
x * 2;
like the math
This function accepts an integer argument and yields its double.
A function
val double = fn x:int =>
x * 2;
like the math
The keyword val binds a value to the variable, in this case the value is a function.
A function
val double = fn x:int =>
x * 2;
like the math
The function get a list of arguments and yields the computed result.
Partial application
fun sum x y = x + y;
sum -> is a function
sum 1 3 -> is an integer value
Partial application
sum 2 = ??? WTF
the partial applicationreturns a function that
takes an int and returns an int
fn : int -> int
Partial application
Haskell Curry
fn (x, y, z, ...)
fn x => fn y => fn z => ...
Datatype [ list ]
listof * := Nil | Cons * (listof *)
This line defines a list of * (whatever is *) that could be either Nil (an empty list)or a Cons of * and another list.Example:[] means Nil[1,2] means Cons 1(Cons 2(Cons Nil))
Datatype [ list ]
* *
*
Cons n ( )
Cons n ( )
Cons n ( Nil )
Pattern Matchingdefining functions by cases:
fun and_operator true true = true | and_operator x y = false;
When both arguments are true the result is true, result is false in the other cases.
Sum the elements of listThe elements of the list could be added by a recursive “sum“ function
fun sum Nil = 0 | sum (Cons n list) = n + sum (list)
Sum the elements of listExamining the definition of sum we see that there are only two specific computation parts
fun sum Nil = 0 | sum (Cons n list) = n + sum (list)
Modularizing the function
fun sum Nil = 0 | sum (Cons n list) = n + sum (list)
Modularizing the function
fun sum Nil = 0 | sum (Cons n list) = n + sum (list)
foldr f x Nil = xfoldr f x (Cons n list) =
f n ((foldr f x) list)
Modularizing the function
fun sum Nil = 0 | sum (Cons n list) = n + sum (list)
foldr f x Nil = xfoldr f x (Cons n list) =
f n ((foldr f x) list)
0 is the base value used for the base case (empty list)
Modularizing the function
fun sum Nil = 0 | sum (Cons n list) = n + sum (list)
foldr f x Nil = xfoldr f x (Cons n list) =
f n ((foldr f x) list)
the + operator is a function
0 is the base value used for the base case (empty list)
Modularizing the function
fun sum Nil = 0 | sum (Cons n list) = n + sum (list)
foldr f x Nil = xfoldr f x (Cons n list) =
f n ((foldr f x) list)
sum == foldr (+) 0
the + operator is a function
0 is the base value used for the base case (empty list)
The “foldr” functionNow we are able to use the “foldr” function
sum = foldr (+) 0
multiply = foldr (*) 1
subtract = foldr (-) 0
The “foldr” functionTest whether any list of boolean is trueanytrue = foldr (or) false
Test all elements are truealltrue = foldr (and) true
These are a functions
Everything is a functionIn a functional world a complete program is a function, so like in the math we could combine programs together.
Combining programs | functionsNow combining the first program with the second one becomes like combining mathematical functions
Math says g • fin Standard MLfun combine f g x = f(g(x));
Functional and the Real World
http://www.leafpetersen.com/leaf/publications/hs2013/hrc-paper.pdf
Thanks
@toretto460