Functions with generic parameter types

Overloading is typically the bugaboo of type-inferenced languages (at least when, like F#, the type system isn’t powerful enough to contain type-classes). There are a number of choices you have in F#: Use overloading on methods (members of a type), in which case overloading works much like as in other .Net languages (you can ad-hoc … Read more

F#, Split String and .Net methods

You call them as instance methods: let Count (text : string) = let words = text.Split [|’ ‘|] let nWords = words.Length (nWords) (Note you need to use [| |] because Split takes an array not a list; or, as per Joel Mueller’s comment, because Split takes a params array, you can just pass in … Read more

F# printf string

That’s because the format parameter is not actually a string. It’s TextWriterFormat<‘T> and the F# compiler converts the string format into that type. But it doesn’t work on string variables, because the compiler can’t convert the string to TextWriterFormat<‘T> at runtime. If you want to print the content of the variable, you shouldn’t even try … Read more

Accessing a specific member in a F# tuple

You want to prevent your wife from aging by making her age immutable? 🙂 For a tuple that contains only two members, you can fst and snd to extract the members of the pair. let wifeName = fst myWife; let wifeAge = snd myWife; For longer tuples, you’ll have to unpack the tuple into other … Read more

F# string.Format

If you want to avoid using the full name, you can use open in F#: open System let s = String.Format(“Hello {0}”, “world”) This should work in both F# interactive (enter the open clause first) and in normal compiled applications. The key thing is that you must write String with upper-case S. This is because … Read more

cons operator (::) in F#

As others said, there is no such operator, because it wouldn’t make much sense. I actually think that this is a good thing, because it makes it easier to realize that the operation will not be efficient. In practice, you shouldn’t need the operator – there is usually a better way to write the same … Read more

F#: String.Join and |> operator

String.Join is a .NET method. When using a .NET method, F# views it as a function that takes a tuple as an argument (when calling it you write parameters as f(a, b)). The |> operator can be used with functions that use the curried form of parameters (and can be called by writing f a … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)