Successfully reported this slideshow.
Upcoming SlideShare
×

# On fuctional programming, high order functions, ML

667 views

Published on

on functional programming, an introductive approach to functional programming

Published in: Technology
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

### On fuctional programming, high order functions, ML

1. 1. Why Functional Programming Matters
2. 2. Why ? Functional 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
3. 3. What is a function The basic, and not very enlightening definition is this: “in a functional language, functions are firstclass citizens.”
4. 4. What is a function The mean is that the function takes a function as one of its arguments. let double x = x * 2 in List.map double [ 1; 2; 3 ];; - : int list = [2; 4; 6]
5. 5. 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.
6. 6. 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.
7. 7. 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 sideeffect.
8. 8. 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.
9. 9. 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. ult m ec or ic lus lcu a
10. 10. A function like the math val double = fn x:int => x * 2; This function accepts an integer argument and yields its double.
11. 11. A function like the math val double = fn x:int => x * 2; The keyword val binds a value to the variable, in this case the value is a function.
12. 12. A function like the math val double = fn x:int => x * 2; The function get a list of arguments and yields the computed result.
13. 13. Partial application fun sum x y = x + y; sum -> is a function sum 1 3 -> is an integer value
14. 14. Partial application sum 2 = ??? WTF the partial application returns a function that takes an int and returns an int fn : int -> int
15. 15. Partial application Haskell Curry fn (x, y, z, ...) fn x => fn y => fn z => ...
16. 16. 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))
17. 17. Datatype [ list ] * Cons n ( * ) Cons n ( * ) Cons n ( Nil )
18. 18. Pattern Matching defining 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.
19. 19. Sum the elements of list The elements of the list could be added by a recursive “sum“ function fun sum Nil = 0 | sum (Cons n list) = n + sum (list)
20. 20. Sum the elements of list Examining 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)
21. 21. Modularizing the function fun sum Nil = 0 | sum (Cons n list) = n + sum (list)
22. 22. Modularizing the function fun sum Nil = 0 | sum (Cons n list) = n + sum (list) foldr f x Nil = x foldr f x (Cons n list) = f n ((foldr f x) list)
23. 23. Modularizing the function fun sum Nil = 0 | sum (Cons n list) = n + sum (list) 0 is the base value used for the base case (empty list) foldr f x Nil = x foldr f x (Cons n list) = f n ((foldr f x) list)
24. 24. Modularizing the function fun sum Nil = 0 | sum (Cons n list) = n + sum (list) 0 is the base value used for the base case (empty list) the + operator is a function foldr f x Nil = x foldr f x (Cons n list) = f n ((foldr f x) list)
25. 25. Modularizing the function fun sum Nil = 0 | sum (Cons n list) = n + sum (list) 0 is the base value used for the base case (empty list) the + operator is a function foldr f x Nil = x foldr f x (Cons n list) = f n ((foldr f x) list) sum == foldr (+) 0
26. 26. The “foldr” function Now we are able to use the “foldr” function sum = foldr (+) 0 multiply = foldr (*) 1 subtract = foldr (-) 0
27. 27. The “foldr” function Test whether any list of boolean is true anytrue = foldr (or) false Test all elements are true alltrue = foldr (and) true These are a functions
28. 28. Everything is a function In a functional world a complete program is a function, so like in the math we could combine programs together.
29. 29. Combining programs | functions Now combining the first program with the second one becomes like combining mathematical functions Math says g • f in Standard ML fun combine f g x = f(g(x));
30. 30. Functional and the Real World http://www.leafpetersen.com/leaf/publications/hs2013/hrc-paper.pdf
31. 31. Thanks @toretto460