Search code examples
performancefunctional-programmingpattern-matchingocamlpreference

Which way of these two pattern matching is more preferred?


I'm just curious, these two functions would do the same thing. But which one should I use?

let f a =
    match a with
        b -> a;; 
let f a =
    match a with
        b -> b;;

Or it just depends on your preference?
I feel the second one would be better but I'm not sure.


Solution

  • Performance wise there is no difference. Style-wise b -> a is a bit problematic because you have an unused variable b. _ -> a would make more sense. Other than that, it's just preference.

    Personally I would prefer _ -> a over b -> b because it doesn't introduce an extra variable.

    PS: I assume in your real code there are more cases than just b - otherwise you could just write let f a = a.