(***********************) (* The Pipe Combinator *) (***********************) let (|>) x f = f x ;; (************************) (* The Pair Combinators *) (************************) let both f (x,y) = (f x, f y);; let do_fst f (x,y) = (f x, y);; let do_snd f (x,y) = ( x, f y);; (**************************) (* Pipelines: An Example *) (**************************) let even x = (x/2)*2 == x;; let process (p : float * float) = p |> both int_of_float (* convert to float *) |> do_fst ((/) 3) (* divide fst by 3 *) |> do_snd ((/) 2) (* divice snd by 2 *) |> both even (* test for even *) |> fun (x,y) -> x && y (* both even *) ;; (********************) (* Grade Processing *) (********************) type student = {first: string; last: string; assign: float list; final: float};; let students : student list = [{first="Sarah"; last="Jones"; assign=[7.0;8.0;10.0;9.0];final=8.5}; {first="Qian"; last="Xi"; assign=[7.3;8.1;3.1;9.0];final=6.5}; {first="Naga"; last="Katta"; assign=[7.3;8.1;3.5;9.0];final=7.0} ] ;; (* Task: Create a function display that does the following: * for each student, print the following: * last name, first name: score * * where score is computed by averaging the assignments and giving the * assignment average 1/2 the weight of the final * * the students are sorted by score and displayed *) let compute_score {first=f; last=l; assign=grades; final=exam} = let score grades exam = List.fold_right (fun x (number,total) -> (number+1,total+.x)) grades (0,0.0) in let (number, total) = score grades exam in (f, l, total /. float_of_int number) ;; let student_compare (_,_,score1) (_,_,score2) = if score1 < score2 then 1 else if score1 > score2 then -1 else 0 ;; let stringify (first, last, score) = last ^ ", " ^ first ^ ": " ^ string_of_float score;; let display (ss : student list) : unit = ss |> List.map compute_score |> List.sort student_compare |> List.map stringify |> List.iter print_endline ;;