Advanced Functional Prog.

back - up - next


Lists' processing

  1. Type generalization/parameter
type 't Stack = NewStack | Push of 't*('t Stack);;

let s1 : int    Stack = Push (3,Push(2,Push(1,NewStack)));;
let s2 : string Stack = Push ("uha.fr",Push("google.fr",NewStack));;
  1. Function generalization (higher-order)
let rec reduceStack v f s = 
  match s with
  | NewStack     -> v
  | Push (v',s') -> f v' (reduceStack v f s');;

reduceStack NewStack (fun x y->Push(x,y)) s2;;               // id
reduceStack s2 (fun x y->Push(x,y)) s2;;                     // append
reduceStack NewStack (fun x y->Push(String.length x,y)) s2;; // map
...
reduceStack [] (fun x y->x::y) s2;;                          // isomorphism

Nb. F# can use all the .net API with in particular C# libraries (see String.length method above).


8