This commit is contained in:
Rhiannon Morris 2020-12-06 10:13:36 +01:00
parent ef1719c280
commit 812fa7498c
9 changed files with 2344 additions and 14 deletions

17
misc.ml
View file

@ -26,16 +26,23 @@ let%test _ = fold_list add [1;2;3;4] = 10
let%test _ = fold_list mult [1;2;3;4] = 24
let print_fold mon xs =
let print_fold ?(format=false) mon xs =
let res = List.fold_left mon.op mon.id xs in
let rec go fmt = function
| [] -> Format.fprintf fmt "%a" mon.pp mon.id
| [x] -> Format.fprintf fmt "%a" mon.pp x
| x::xs -> Format.fprintf fmt "%a %s %a" mon.pp x mon.op_name go xs in
Format.printf "%a = %a\n" go xs mon.pp res
| x::xs ->
let f =
if format then Format.fprintf fmt "%a %s@ %a"
else Format.fprintf fmt "%a %s %a" in
f mon.pp x mon.op_name go xs in
let f =
if format then Format.printf "%a =@ %a\n"
else Format.printf "%a = %a\n" in
f go xs mon.pp res
let print_prod = print_fold mult
let print_sum = print_fold add
let print_prod ?format = print_fold ?format mult
let print_sum ?format = print_fold ?format add
let%expect_test _ =
print_prod [];