3 ms·
That's fair. Assuming I introduced a new macro "compactexpr" it could have been written: [%%compactexpr "SumArStaged"; [ arr; n ]; let sum = ref 0 in
by beckford 3y ago
That's fair. Assuming I introduced a new macro "compactexpr" it could have been written:
[%%compactexpr
"SumArStaged";
[ arr; n ];
let sum = ref 0 in
for i = 0 to 3 do
for j = i to min (i + 3) (n - 1) do
sum := !sum + arr.(j)
done
done;
!sum]
;;
print_c "sum_ar" (module SumArConv) SumArStaged.expr;;
So that has the exact indent level of the C code. (Details: The above is formatted using the "conventional" profile for the "ocamlformat" tool)
Of course, there is a disadvantage. I had to use global variables in the above "compactexpr". Global variables mimic the C language (ie. C functions are globally-scoped or file-scoped functions) but wouldn't be a good fit when translating that code to functional languages. Additionally, the OCaml idiom `fun arr n ->` was lost, but that seems minor.
Do you and others find the above readable? (If so, perhaps it would be best to give some flexibility for the coding style.)