(* ---------- ex26 ---------- *) type matrix = int list list ;; let rec suml l = match l with | [] -> 0 | el::xl -> el + suml xl ;; let rec sum m = match m with | [] -> 0 | []::xm -> sum xm | [v::vs] -> v + suml vs | (v::vs)::xm -> v + suml vs + sum xm ;; sum [[1; 2; 3]; [4; 5; 6]] ;; sum [[1; 2; 3]; []; [4; 6]] ;; sum [[1; 2; 3]; []; [4; 5; 6]] ;; let rec mxl m = match m with | [] -> 0 | [el] -> el | el::xl -> max (el) (mxl xl) ;; let rec mx m = match m with | [] -> 0 | []::xm -> 0 | [v::vs] -> max v (mxl vs) | (v::vs)::xm -> max (max v (mxl vs)) (mx xm) ;; mx [[1; 2; 8]; [4; 6]] ;; mx [[9; 2; 3]; [4; 5; 6]] ;; mx [[1; 2; 3]; [4; 5; 6]] ;; let rec makel x = if (x = 0) then [] else 0 :: makel (x-1) ;; let rec make x y = if (y = 0) then [] else makel x :: make x (y-1) ;; make 2 2 ;; make 3 5 ;; make 0 0 ;; make 1 1 ;; let rec showl l = match l with | [] -> () | el::xl -> Printf.printf "%4d" el ; showl xl ;; let rec show m = match m with | [] -> () | []::xm -> () ; show xm | (v::vs)::xm -> Printf.printf "%4d" v ; showl vs ; print_string "\n" ; show xm ;; show [[1; 2; 3]; [4; 5; 6]] ;; show [[1; 2; 3]; [4; 5; 6]; [7; 8; 9]] ;; let rec loadLine n ci = if n = 0 then [] else let line = int_of_string (input_line ci) in line :: loadLine (n-1) ci ;; let rec loadMatrix m n ci = if m = 0 then [] else let line = loadLine n ci in line :: loadMatrix (m-1) n ci ;; let rec load ni = let ci = open_in ni in let m = int_of_string (input_line ci) in let n = int_of_string (input_line ci)in let res = loadMatrix m n ci in close_in ci ; res ;; load "matrix.txt" ;;