Pattern matching on the beginning of a string in f#

Parameterized active patterns to the rescue! let (|Prefix|_|) (p:string) (s:string) = if s.StartsWith(p) then Some(s.Substring(p.Length)) else None match “Hello world” with | Prefix “The” rest -> printfn “Started with ‘The’, rest is %s” rest | Prefix “Hello” rest -> printfn “Started with ‘Hello’, rest is %s” rest | _ -> printfn “neither”

Explaining pattern matching vs switch

Having formerly been one of “those people”, I don’t know that there’s a succinct way to sum up why pattern-matching is such tasty goodness. It’s experiential. Back when I had just glanced at pattern-matching and thought it was a glorified switch statement, I think that I didn’t have experience programming with algebraic data types (tuples … Read more

Application architecture/composition in F#

This is easy once you realize that Object-Oriented Constructor Injection corresponds very closely to Functional Partial Function Application. First, I’d write Dings as a record type: type Dings = { Lol : string; Rofl : string } In F#, the IGetStuff interface can be reduced to a single function with the signature Guid -> seq<Dings> … Read more

Use of `inline` in F#

The inline keyword indicates that a function definition should be inserted inline into any code which uses it. Most of the time, this will not have any effect on the type of the function. However, in rare cases, it can lead to a function which has a more general type, since there are constraints which … Read more

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

What the difference between a namespace and a module in F#?

A namespace is a .Net thing, common in many industrial-strength languages, just a way to organize frameworks and avoid naming conflicts among different libraries. Both you and I can define a type “Foo” and use them both in a project, provided they are in different namespaces (e.g. NS1.Foo and NS2.Foo). Namespaces in .Net contain types. … Read more

F# changes to OCaml [closed]

This question has been answered for some time now, but I was quite surprised that most of the answers say what OCaml features are missing in F# – this is definitely good to know if you want to port existing OCaml programs to F# (which is probably the motivation of most of the referenced articles). … Read more

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