F#: let mutable vs. ref

I can only support what gradbot said – when I need mutation, I prefer let mutable. Regarding the implementation and differences between the two – ref cells are essentially implemented by a very simple record that contains a mutable record field. You could write them easily yourself: type ref<‘T> = // ‘ { mutable value … Read more

Why is this F# code so slow?

The problem is that the min3 function is compiled as a generic function that uses generic comparison (I thought this uses just IComparable, but it is actually more complicated – it would use structural comparison for F# types and it’s fairly complex logic). > let min3(a, b, c) = min a (min b c);; val … Read more

Pattern matching identical values

This is called a nonlinear pattern. There have been several threads on the haskell-cafe mailing list about this, not long ago. Here are two: http://www.mail-archive.com/[email protected]/msg59617.html http://www.mail-archive.com/[email protected]/msg62491.html Bottom line: it’s not impossible to implement, but was decided against for sake of simplicity. By the way, you do not need if or case to work around this; … Read more

Global operator overloading in F#

Here is the (non idiomatic) solution: type Mult = Mult with static member inline ($) (Mult, v1: ‘a list) = fun (v2: ‘b list) -> v1 |> List.collect (fun x -> v2 |> List.map (fun y -> (x, y))) : list<‘a * ‘b> static member inline ($) (Mult, v1:’a ) = fun (v2:’a) -> v1 … Read more

Using keywords as identifiers in F#

Given section 3.4 of the F# 2.0 spec: Identifiers follow the specification below. Any sequence of characters that is enclosed in double-backtick marks (“ “), excluding newlines, tabs, and double-backtick pairs themselves, is treated as an identifier. I suspect you can put it in backticks: “private“ I haven’t tried it though.

Why isn’t there a protected access modifier in F#?

The protected modifier can be quite problematic in F#, because you often need to call members from a lambda expression. However, when you do that, you no longer access the method from within the class. This also causes confusion when using protected members declared in C# (see for example this SO question). If you could … Read more