The order in which the arguments are evaluated is not specified in OCaml. Therefore, when you execute fx :: gy , it is not fx :: gy whether the call to f or g first. In your case, the recursive call is called before the bscanf call, so you get the results in the wrong order.
A common way to troubleshoot evaluation problems is to put function arguments in local variables when the order of their side effects matters. Therefore, instead of fx :: gy you should do let fx = fx in fx :: gy if you want fx effects to be executed before g called.
However, in your case, you can simply use the bscanf continuation bscanf as follows:
bscanf sb " %d" (fun a -> a :: int_list_from_sb sb (n - 1))
source share