Next in the F# language, I would like to go over arrays.
I went over loops in my previous post, which I will be using which our arrays, so please take a look at that first if you haven’t learned how to do a for statement. One thing to remember about Arrays is that they are mutable by default, so you don’t have to use the mutable keyword to indicate that you would like to update the values.
The syntax for declaring an array seems a little strange to me coming from a C# background, and that there are different ways to declare an array with static data.
In continuing our look at F#, I’d like to look at flow control, like conditional and looping of logic.
Before I get to that, I mentioned in my previous post, that F# uses a type inference, which means the compiler can guess which type the variables and methods are. However, you can also explicitly declare a variable type.
For example, here is a function that takes in a string parameter, we are declaring the input as a string type. You can see how the function is called and passed input parameters from the EntryPoint main method:
let SayHello (input : string) =
printfn "Hello, %s" (input)
let main argv =
System.Console.ReadKey(false) |> ignore
0 // return an integer exit code
I wanted to pick up a new programming language and learn as I go along, which will hopefully let you learn as well. I have decided to learn F#. I was going to do Python, and probably will but I found a great blog that has already done this using Python. I would like to do a comparison between these languages as I know they are similar and F# was influenced by Python. As of now, I still don’t have a good reason to learn F#, but it’s syntax looks similar to Lambda expressions in C#, so it hopefully will open my mind more to functional languages. I took Scheme in college and found it to be a lot of fun, but not a very useful language, compared to a more object oriented based language.
The first thing I notice about F# is its succinctness. It uses Type Inference, which means the compiler will figure out what type a variable is, or what a function will return. So you don’t have to declare the types.
I can really see the functional programming style of Lambda (=>) expressions in C#.
Compatibility switches is a new feature in the .NET Framework 4.6 which will be useful for Windows App store applications. The .NET Framework only provided compile time features for supporting different versions. While this is a good way to support multiple features, it is not dynamic enough with the different formats and features that Windows applications will need to support today without using a custom solution.
For just a quick overview, the compilation version features are either using Preprocessor Directives and complication symbols, and the System.Diagnostics Conditional features seen below.