Advanced Functional Prog.


Introduction

One benefits of FP is its capability to pass other functions has parameters:

let rec func list func'  = 
  match list with
    | []      -> []
    | (x::xs) -> (func' x)::(func xs func')

printfn "%A" (func list (fun x->x+1))

A second noticeable point is the use of "anonymous" functions.

Questions: what will be printed by the previous expression ? What is the type of "func" now ?

Note. The function used as the example already exists and is called "map":

let func = List.map;;
func (fun x->x+1) [1;2;3];;

For more information about the benefit of such function, take a look at Google MapReduce technology: parallelism, distribution...

Exercice: play a lot with the functions proposed by the List module ... and try to do the functions by yourself

List.append [1;2] [3]
List.find (fun x->x%2=0) [1;2;3]
List.fold (+) 0 [1;2;3]
List.fold (min) 9 [1;2;3]
...

4 - 9