Re:OCaml as the Universal Translator – DkSDK Merry Christmas...
by beckford on Tuesday December 26, 2023 @08:05PM
(#38777890)
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.)
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.)