4

我只是好奇,这两个函数会做同样的事情。但是我应该使用哪一个?

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

还是仅取决于您的喜好?
我觉得第二个会更好,但我不确定。

4

2 回答 2

8

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.

于 2013-02-17T01:22:31.033 回答
1

另外,在您的特定示例中,我将使用function

let f = function
      | b -> b    
于 2013-02-17T19:02:31.963 回答