advertisement

On fuctional programming, high order functions, ML

50 %
50 %
advertisement
Information about On fuctional programming, high order functions, ML
Technology

Published on February 26, 2014

Author: SimoneDiMaulo

Source: slideshare.net

Description

on functional programming, an introductive approach to functional programming
advertisement

Why Functional Programming Matters

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

What is a function The basic, and not very enlightening definition is this: “in a functional language, functions are firstclass citizens.”

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]

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 sideeffect.

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. ult m ec or ic lus lcu a

A function like the math val double = fn x:int => x * 2; This function accepts an integer argument and yields its double.

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.

A function like the math val double = fn x:int => x * 2; 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 application returns 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 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.

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)

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)

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 = x foldr 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) 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)

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)

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

The “foldr” function Now we are able to use the “foldr” function sum = foldr (+) 0 multiply = foldr (*) 1 subtract = foldr (-) 0

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

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

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

Functional and the Real World http://www.leafpetersen.com/leaf/publications/hs2013/hrc-paper.pdf

Thanks @toretto460

Add a comment

Related presentations

Presentación que realice en el Evento Nacional de Gobierno Abierto, realizado los ...

In this presentation we will describe our experience developing with a highly dyna...

Presentation to the LITA Forum 7th November 2014 Albuquerque, NM

Un recorrido por los cambios que nos generará el wearabletech en el futuro

Um paralelo entre as novidades & mercado em Wearable Computing e Tecnologias Assis...

Microsoft finally joins the smartwatch and fitness tracker game by introducing the...

Related pages

Map (higher-order function) - Wikipedia, the free encyclopedia

... map is the name of a higher-order function that applies a ... The map function originated in functional programming ... Standard ML: map func ...
Read more

Functional programming - Wikipedia, the free encyclopedia

Programming in a functional style can ... higher-order functions. ... to be associated with functional programming. In the 1970s, ML was created ...
Read more

Functional Programming - History

... higher-order functions. ... function-level programming rather than the lambda-calculus style which has come to be associated with functional ...
Read more

Functional Programming in ML - Welcome to Yuh-Jzer Joung's ...

Lists are the original data structure of functional programming, ... 2007 Functional Programming in ML 9 FUNCTIONS ON ... A function is higher order if ...
Read more

functional programming - VBScript Higher-Order Functions ...

VBScript Higher-Order Functions. ... For specific problems that can be solved with higher order functions in functional ... Does Functional Programming ...
Read more

Functional C Programming: Higher order functions, closures ...

... dr You can implement many functional programming ... C Programming: Higher order functions, ... Functional C Programming: Higher order ...
Read more

Recitation 3: Higher order functions

Higher-order and Anonymous Functions, Currying. ... Functions that return other functions are so common in functional programming that ML provides a ...
Read more

What advantages do functional programming languages ...

... Functional Programming (FP) ... Standard ML, Erlang, Scala, ... I'm also talking about higher order functions.
Read more

functional programming - using callCC with higher-order ...

using callCC with higher-order functions ... The trick to making higher-order functions work with callCC is to ... Functional programming in C with macro ...
Read more