function returns list in reverse order in OCaml

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-01 18:03:22

The order of evaluation of arguments is unspecified in OCaml. So when you do f x :: g y, it is unspecified whether f or g gets called first. In your case the recursive call is invoked before the call to bscanf, which is why you get the results in the wrong order.

The general way to fix evaluation-order issues is to put the arguments to a function into local variables when the order of their side effects matters. So instead of f x :: g y, you'd do let fx = f x in fx :: g y if you want the effects of f x to happen before g is called.

However in your case you can just make use of bscanf's continuation argument like this:

bscanf sb " %d" (fun a -> a :: int_list_from_sb sb (n - 1))
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!