Will OCaml convert multi-argument function to currying or the other way around?

前端 未结 4 1497
醉梦人生
醉梦人生 2021-01-13 01:53

When I was learning OCaml essentials, I was told that every function in OCaml is actually a function with only one parameter. A multi-argument function is actually a functio

4条回答
  •  终归单人心
    2021-01-13 02:33

    Okay, I learned that the native compiler will optimize your code, what I expect it to do. But here is the bytecode compiler:

    let plus1 x y = x + y
    let plus2 = fun x y -> x + y
    let plus3 = function x -> function y -> x + y
    

    treated with ocamlc -c -dinstr temp.ml gives me:

           branch L4
            restart
    L1:     grab 1
            acc 1
            push
            acc 1
            addint
            return 2
            restart
    L2:     grab 1
            acc 1
            push
            acc 1
            addint
            return 2
            restart
    L3:     grab 1
            acc 1
            push
            acc 1
            addint
            return 2
    

    which means the result is exactly the same, it is only a syntax difference. And the arguments are taken one by one.

    Btw, one more syntax point: fun can be written with n arguments, function only with one.

    From the conceptual point of view I would largely favor function x -> function y -> over the others.

提交回复
热议问题