Checking the sample code
(Contributed by Djoums):
Looking at the syntax
- This is very different from an imperative style code! A proper introduction to functional programming (FP) is unfortunately beyond the scope of this playground.
- Single line comments can be declared with
//, but multi-line comments are between
*). Don't ask...
|>is the pipe forward operator, used to chain the function calls. Each call receives the result of the previous one as a parameter.
- There are some function names (
PadLeft) which look exactly the same as in our C# solution. This is not a coincidence. As C#, F# and VB .NET are all targeting Microsoft's Common Language Infrastructure (CLI) on .NET Core, they share some of the standard libraries.
- Unlike the
usingstatement in C#, F# uses
open. This comes from a design choice of organizing functions in
fun x ->defines an anonymous lambda function on the fly.
elseis not a conditional statement, but a conditional expression so it returns a result.
inis equivalent to a new line after a
let, allowing you to write several instructions in one go.
Collections are usually very important in F#, here are some points worth noting:
Listsin F# are very different from their C# counterpart. They are immutable singly-linked lists designed for enumeration, but not for direct indexing (you can, but performance will be horrendous).
Arraysare your usual fixed-size contiguous-memory fast access collections. They are not immutable however. Note that we used an array map call in our example with
Sequencesare an abstraction that represents a collection, which doesn't care about how the collection is implemented under the hood. They are also Lazy, making them very similar to
IEnumerablein C#. We also used a map call on a sequence in our example, but this time it was with
- You can of course create your own collections.
F# defines itself as "functional-first" programming language. Unlike Haskell, which is pure uncompromising FP, with F# you can mix in some imperative or object-oriented style in your code, when it makes sense.
However, this can be abused. To demonstrate, let us look at an F#
Chuck Norris solution in imperative style. Don't try this at home! :-)
Resources to check
The natural choice for the next section is the "most-FP language of all FP languages", Haskell...