Advanced Functional Prog.


Direct mail (aka "publipostage")

// csv := (v(,v)*\n)*
let db = [["bill";"12";"18"];["kate";"17";"15"]];;

// db |> map template |> reduce untemplate

let template [n;n1;n2] = 
  "<html><body>
   <h1>"+n+"</h1>
   <ul>
    <li>"+n1+"</li>
    <li>"+n2+"</li>
   </ul>
   </body></html>";;

List.map template db |> List.iter (printfn "%s");;

type Node = Tag of string*(Node list) | Text of string;;
let template [n;n1;n2] = 
  Tag ("html",[Tag ("body",
    [Tag ("h1",[Text n]); 
     Tag ("ul",
       [Tag ("li",[Text n1]);
        Tag ("li",[Text n2])])])]);;

let rec tostr = function
  | Text t -> t
  | Tag (v,vs) -> "<"+v+">"+(List.reduce (+) (List.map tostr vs))+"<"+v+"/>";;

List.map (template >> tostr) db;; 

let untemplate (Tag ("html",[Tag ("body",[Tag ("h1",[Text n]); Tag ("ul",[Tag ("li",[Text n1]);Tag ("li",[Text n2])])])])) = [n;n1;n2];;
template ["bill";"12";"18"] |> untemplate;;