Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our User Agreement and Privacy Policy.

Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our Privacy Policy and User Agreement for details.

Successfully reported this slideshow.

Like this presentation? Why not share!

- ML Tutorial Introduction by elbop 427 views
- Modelling of walking humanoid robot... by ijfcstjournal 441 views
- Coloured petrinets by lovebot 386 views
- Standard ML / CPN ML by Dalton Valadares 2368 views
- Colored petri nets theory and appli... by Abu Hussein 3991 views
- Modeling and Analysis of Reliabilit... by Editor IJCATR 379 views

662 views

Published on

on functional programming, an introductive approach to functional programming

Published in:
Technology

No Downloads

Total views

662

On SlideShare

0

From Embeds

0

Number of Embeds

9

Shares

0

Downloads

8

Comments

0

Likes

2

No embeds

No notes for slide

- 1. Why Functional Programming Matters
- 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. What is a function The basic, and not very enlightening definition is this: “in a functional language, functions are firstclass citizens.”
- 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. 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. 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. 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. 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. 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. A function like the math val double = fn x:int => x * 2; This function accepts an integer argument and yields its double.
- 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. 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. Partial application fun sum x y = x + y; sum -> is a function sum 1 3 -> is an integer value
- 14. Partial application sum 2 = ??? WTF the partial application returns a function that takes an int and returns an int fn : int -> int
- 15. Partial application Haskell Curry fn (x, y, z, ...) fn x => fn y => fn z => ...
- 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. Datatype [ list ] * Cons n ( * ) Cons n ( * ) Cons n ( Nil )
- 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. 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. 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. Modularizing the function fun sum Nil = 0 | sum (Cons n list) = n + sum (list)
- 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. 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. 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. 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. The “foldr” function Now we are able to use the “foldr” function sum = foldr (+) 0 multiply = foldr (*) 1 subtract = foldr (-) 0
- 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. 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. 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. Functional and the Real World http://www.leafpetersen.com/leaf/publications/hs2013/hrc-paper.pdf
- 31. Thanks @toretto460

No public clipboards found for this slide

Be the first to comment