Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 221
octref Avatar asked Jan 15 '23 05:01

octref


1 Answers

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.

like image 101
sepp2k Avatar answered Apr 27 '23 02:04

sepp2k