I've got a function in C#, and I'd like to port it (among some other stuff) over to F#, just for the sake of doing it. Unfortunately, I just hit a case for which there seems to be no way to express this in F#: Take this C# function
public static T Min<T>(params T[] p) where T : IComparable
{
T m1 = p[0];
foreach (T v in p)
{
m1 = (m1.CompareTo(v) < 0) ? m1 : v;
}
return m1;
}
I'd thought this would be pretty easy, but I don't understand how I would specify a variable argument list in F#. I have tried this:
let rec Min l =
match l with
| [] -> 0 // should throw exception here
| [v] -> v
| (h::t) -> min h (Min t)
but calling that from C# expects a Microsoft.FSharp.Collections.List
. Is it possible to get it expect a params T[]
, and if so, how?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…