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:
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.
[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 [<EntryPoint>] 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 World\r\n\t!" 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. prefixFunction // 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:
Spoiler
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
Page 1 of 1
Trackbacks for this entry [ Trackback URL ]
← January 2021 →
S | M | T | W | T | F | S |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
31 |
Tags
My Blog Links
Recent Entries
Recent Comments
Search My Blog
0 user(s) viewing
0 Guests
0 member(s)
0 anonymous member(s)
0 member(s)
0 anonymous member(s)