I found a presentation from Don Syme that shows that
Erlang <
fac(0) -> 1 fac(N) -> N * fac(N-1).
equivalent to f # 's
let rec fac = function | 0 -> 1 | n -> n * fac (n-1)
But it seems that there is no way to use pattern matching for another arrangement without losing type safety. For example. you can use list template matching, but then the type must be a common base type (for example, object ):
let concat = function | [x;y] -> x.ToString() + y.ToString() | [x] -> x.ToString()
Given that F # functions in modules do not support overloads, it looks like the only way to rewrite Erlang code in F # with static typing is to use static classes with method overloads instead of modules. Is there a better way to rewrite Erlang functions with different arity in F #?
In general, is it correct to say that mapping Erlang arguments is closer to overloading the .NET method (including C #), rather than matching to an F # pattern? Or there is no direct replacement between them, for example. in Erlang, there may be a function with different levels + guard:
max(x) -> x. max(x,y) when x > y -> x. max(x,y) -> y. max(comparer, x, y) -> if comparer(x,y) > 0 -> x; true -> y end.
In the latter case, the arguments are of different types. How would you rewrite it in F #?
erlang f #
Vb
source share