Posts

Showing posts with the label f#

F# and the immediate window

(This post is inspired from t his Stack Overflow question ) F# debugger integration is quite good, except for the immediate window. The immediate window requires C# style syntax, so calling F# functions is not intuitive. Below are recipes for the immediate window integrated into a console application. As always code can be found here : // Learn how to use the immediate window for F#. module Program= // To build, add a reference to Microsoft.CSharp. This is required for dynamic support in // the debugger. let allowDynamicToWorkFromImmediateWindowByLoadingCSharpBinders = Microsoft.CSharp.RuntimeBinder.CSharpBinderFlags.BinaryOperationLogical let plusOne = (+)1 let xPlusOnes = [1;2;3] |> Seq.map plusOne [ ] let main (args) = printfn "Try the following in the immediate window: // plusOne 4 ((dynamic)plusOne).Invoke(4) // Seq.Head xPlusOnes Microsoft.FSharp.Collections.SeqModule.Head(xPlusOnes) // let xPlusTwos= xP...

F# Tricks: Concise BoolToInt

As a C# programmer, you'd probably write BoolToInt as follows in F#: let boolToInt3 b = if b = true then 1 elif b = false 0 Not a big win over C# yet, but a few things to point out about the concise syntax: Indentation implies blocks. There is no 'return' keyword. This function is statically typed, you don't see types since the compiler infers the types. As a novice F# programmer a trick you learn is pattern matching. Pattern matching is like a case statement, but it's an expression and it's really powerful. I'll go into more details on pattern matching in future posts. For now, notice the elegance of the below: let boolToInt2 b = match b with | true -> 1 | false -> 0 Pattern matching is so useful to F# there is a syntax for creating a function of one parameter that only pattern matches, using that syntax, we define boolToInt in the final form of: let boolToInt3 = function | true -> 1 | false ->...