on fuctional programming, high order functions, ml

31
Why Functional Programming Matters

Upload: toretto460-di-maulo

Post on 11-May-2015

366 views

Category:

Technology


6 download

DESCRIPTION

on functional programming, an introductive approach to functional programming

TRANSCRIPT

Page 1: On fuctional programming, high order functions, ML

WhyFunctional Programming

Matters

Page 2: On fuctional programming, high order functions, ML

“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

?

Page 3: On fuctional programming, high order functions, ML

What is a function

The basic, and not very enlightening definition is this: “in a functional language, functions are first-class citizens.”

Page 4: On fuctional programming, high order functions, ML

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]

Page 5: On fuctional programming, high order functions, ML

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.

Page 6: On fuctional programming, high order functions, ML

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.

Page 7: On fuctional programming, high order functions, ML

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.

Page 8: On fuctional programming, high order functions, ML

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.

Page 9: On fuctional programming, high order functions, ML

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

Page 10: On fuctional programming, high order functions, ML

A function

val double = fn x:int =>

x * 2;

like the math

This function accepts an integer argument and yields its double.

Page 11: On fuctional programming, high order functions, ML

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.

Page 12: On fuctional programming, high order functions, ML

A function

val double = fn x:int =>

x * 2;

like the math

The function get a list of arguments and yields the computed result.

Page 13: On fuctional programming, high order functions, ML

Partial application

fun sum x y = x + y;

sum -> is a function

sum 1 3 -> is an integer value

Page 14: On fuctional programming, high order functions, ML

Partial application

sum 2 = ??? WTF

the partial applicationreturns a function that

takes an int and returns an int

fn : int -> int

Page 15: On fuctional programming, high order functions, ML

Partial application

Haskell Curry

fn (x, y, z, ...)

fn x => fn y => fn z => ...

Page 16: On fuctional programming, high order functions, ML

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))

Page 17: On fuctional programming, high order functions, ML

Datatype [ list ]

* *

*

Cons n ( )

Cons n ( )

Cons n ( Nil )

Page 18: On fuctional programming, high order functions, ML

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.

Page 19: On fuctional programming, high order functions, ML

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)

Page 20: On fuctional programming, high order functions, ML

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)

Page 21: On fuctional programming, high order functions, ML

Modularizing the function

fun sum Nil = 0 | sum (Cons n list) = n + sum (list)

Page 22: On fuctional programming, high order functions, ML

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)

Page 23: On fuctional programming, high order functions, ML

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)

Page 24: On fuctional programming, high order functions, ML

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)

Page 25: On fuctional programming, high order functions, ML

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)

Page 26: On fuctional programming, high order functions, ML

The “foldr” functionNow we are able to use the “foldr” function

sum = foldr (+) 0

multiply = foldr (*) 1

subtract = foldr (-) 0

Page 27: On fuctional programming, high order functions, ML

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

Page 28: On fuctional programming, high order functions, ML

Everything is a functionIn a functional world a complete program is a function, so like in the math we could combine programs together.

Page 29: On fuctional programming, high order functions, ML

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));

Page 30: On fuctional programming, high order functions, ML

Functional and the Real World

http://www.leafpetersen.com/leaf/publications/hs2013/hrc-paper.pdf

Page 31: On fuctional programming, high order functions, ML

Thanks

@toretto460