Functional Programming & Proofs


Trees and languages

  1. (map f)
    How to apply a function f to all the value of a stream ?
let rec map f s = 
  match s with 
  | End        -> End
  | Add (v,s1) -> Add (f v,map f s1);;

map ((+) 1) s;; 

How to define "generic" transformation called reduce (similar to eval on expressions) on streams ?
How to use it to define differently map ? And cat ?
What will be the value of reduce 0 max s ?

let rec reduce fend fadd s = 
  match s with 
  | End        -> fend
  | Add (v,s1) -> fadd v (reduce fend fadd s1);;

reduce End (fun x y->Add((+) 1 x,y)) s;;
reduce s   (fun x y->Add(x,y))       s;;
reduce 0    max                      s;;

7 - 10