Subscribe to andrewsw's Blog        RSS Feed

Dipping Into F#

Icon 2 Comments
If you don't have access to a full version of Visual Studio it is now easier to dip your toe into the F# waters, and functional programming, by downloading the free Visual Studio Community 2013 edition. With this, F# appears in the New Projects' dialog and you're good to go!

[Previously you had to download some files, and either run from a command prompt or configure your editor/IDE, or install an odd little editor named Tsunami. Use F# on Windows]

I was just wandering through the free ebook from Syncfusion. I like these books. It is a good introduction so far, although I've only gone part-way through it. Microsoft has Try F# as well. This isn't bad but you have to go through a number of pages and then realise that the tutorial suddenly stops.

Here's some code from the ebook to give a flavour of what F# code looks like:
open System

let myAdd = fun x y -> x + y
// short syntax
let raisePowerTwo x = x ** 2.0

let main argv = 
    //printfn "%A" argv
    Console.WriteLine "Hello World!"

    let x = 42
    // this looks like a variable assignment, but it is an "identifier"
    let x' = 50     // apostrophe is allowed as part of identifier name
    let ``more?`` = true        // double back-ticks to quote odd name

    // string literals
    let message = "Hello
    let dir = @"c:\users"        // verbatim

    let n = 10
    let add a b = a + b
    let result = add n 4
    printfn "%i" (result)       // 14

    let halfWay a b =
        let dif = b - a
        let mid = dif / 2
        mid + a

    printfn "%i" (halfWay 10 20)

    // Function that returns a function to
    let calculatePrefixFunction prefix =
        // calculate prefix.
        let prefix' = Printf.sprintf "[%s]: " prefix
        // Define function to perform prefixing.
        let prefixFunction appendee =
            Printf.sprintf "%s%s" prefix' appendee
        // Return function.

    // Create the prefix function.
    let prefixer = calculatePrefixFunction "DEBUG"
    // Use the prefix function.
    printfn "%s" (prefixer "My message")

    // A function to generate the Fibonacci numbers.
    // 'rec' keyword == recursive
    let rec fib x =
        match x with
        | 1 -> 1
        | 2 -> 1
        | x -> fib (x - 1) + fib (x - 2)

    printfn "(fib 2) = %i" (fib 2)
    printfn "(fib 6) = %i" (fib 6)
    printfn "(fib 11) = %i" (fib 11) 

    // + operator (prefix or infix)
    let rhyme = "Jack" + " and " + "Jill"
    printfn "%s" rhyme
    let oneYearLater = 
        DateTime.Now + new TimeSpan(365, 0, 0, 0, 0)
    printfn "%A" oneYearLater

    let addResult = (+) 2 2
    let addition = (+)

    // function composition
    let add2 x y = x + y
    let result1 = 
        add2 (add2 4 5) (add2 6 7)
    // pipe-forward operator |>
    let resultCos = 0.5 |> System.Math.Cos
    let result2 = add2 6 7 |> add2 4 |> add2 5

    ignore (Console.ReadLine())
    0 // return an integer exit code

The ebook has, early on, a few (small) examples using FSharp.Charting which I installed via NuGet. However, I couldn't quite get it working, even from a script-file (which it seems to require), mainly, perhaps, because VS says it is deprecated. What I've done for the moment is add (via Solution Explorer) a script file to my Console Application and copy the following code into this, which I found at an SO topic:


I then selected this code and used Alt-Enter to run it interactively. This displayed a nice chart. I'm happy with this for the moment; I'll revisit charting later, and see how it is supposed to work.

I'm not even sure at the moment of the difference between a Script and an Application. I've only been tinkering for a couple of hours.

I've tinkered, briefly, with functional programming before, but I've never gotten into it too deeply. I'm not sure how far I will pursue it this time either!? It interests me, and is challenging, but.. I keep putting it off in favour of something else.

Nevertheless, something occurred to me. It is probably too trivial to call an "inspiration" or "eureka" moment, but here goes. I think I was too tied-up with trying to compare programming in F# with creating a Console or Form Application (in C# or VB). This inhibits progress. Create a file or Project (VS only offers a Console Application) and add in (open) the libraries/namespaces you need depending on the direction you want to head.

Because of this I might even abandon Visual Studio and just use something like Notepad++. Coming full circle!

Secondly, and I don't know if this is more, or less, inspirational than my previous revelation, is to forget about imperative or OOP languages. Functional programming is to write a number of identifiers: essentially, let statements. These define, and construct, the parameters for your solution. You are essentially breaking down your program into a sequence of statements. The statements do not need to be in any particular order, although you cannot forward-reference: you have to define something before using it (although, I haven't gone far enough to discover any kind of prototyping or other forward-referencing mechanism).

There needs to be some kind of entry-point, to kick everything off, but this is probably at the end of the code, and probably straight-forward in comparison to the statements that define the program (the problem domain?).

Please don't take the statements just above too seriously - ignore them if I'm confusing you. They are just some early, and personal, impressions.

2 Comments On This Entry

Page 1 of 1


05 February 2015 - 08:44 AM
Is there a strong business case for F#?


06 February 2015 - 04:59 PM
That is not something I address, or intended to address, in my blog-entry. I encourage people to add further comments though, if they wish to respond to your question.

(Could be interesting as a topic in the FP forum ;) )
Page 1 of 1

Trackbacks for this entry [ Trackback URL ]

There are no Trackbacks for this entry

January 2021

171819 20 212223


    Recent Entries

    Recent Comments

    Search My Blog

    0 user(s) viewing

    0 Guests
    0 member(s)
    0 anonymous member(s)