我只是好奇,这两个函数会做同样的事情。但是我应该使用哪一个?
let f a =
match a with
b -> a;;
let f a =
match a with
b -> b;;
还是仅取决于您的喜好?
我觉得第二个会更好,但我不确定。
我只是好奇,这两个函数会做同样的事情。但是我应该使用哪一个?
let f a =
match a with
b -> a;;
let f a =
match a with
b -> b;;
还是仅取决于您的喜好?
我觉得第二个会更好,但我不确定。
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
.
另外,在您的特定示例中,我将使用function
let f = function
| b -> b