Today I started going through the tutorial for F# on www.tryfsharp.org. So far this tutorial is excellent and lets you quickly try F#. The site uses a silverlight plugin which allows you to run F# code in your browser without any IDE. You are up and running within seconds. So far I managed to go through the first section which covers the basics of F#. I will admit some of the constructs so far are nice, not needing to use type annotations because of type inference and the clean syntax of the language (not needing {}, ;, etc). The only complaint so far in the tutorial is with the variable names, they are short and hard to wrap my head around (especially in the currying examples). Anyways here are some random notes I took while going through the first section. Some of this stuff is sort of basic, however typing it up will hopefully make it stick.
- let statements are not assignment statements. Each let statement creates a new association between a name and a value. In functional programming, such an association is called a binding.
-
In F# indentation matters when writing code there is no termination character such as a ; to terminate a line. Also there is no {} characters used if/else blocks as in C#.
C#
if(x < 5){ Console.WriteLine("< 5"); }F#
if x < 5 printfn "< 5" - There is no void return type in F#, instead the return type would be unit.
-
tuple
A tuple is similar to a struct in C#, you can define them with any number of named values, however they should be kept fairly small. You can access the values of a tuple by either fst (first value of a tuple) or snd (second value of a tuple)Where name contains “iMac” and cpu contains “i7”let computer = ("iMac", "i7") let name = fst computer let cpu = snd computer - tuples can not be modified, if you need to alter the values contained in a tuple you need to create a mutable type.
-
Lists
Lists must contain the same type of values. Each item in a list is separated by ;let languages = ["C#"; "F#"; "JavaScript"]
- You can create lists of number with the the “..” operator. let oneToTen = [1 .. 10] will create a list of numbers 1-10, however if you supply two “..” you can specify the number to increment the value. let plusTen = [1 .. 10 .. 100] (e.g. 1, 11, 21, 31…91)
- Lists are 0 based and if you need to access an element of a list you simply use the [] syntax, with the previous example let number = plusTen.[3] will bind 31 to number;
- If you want to add an element to the start of a list use :: let newList = -10 :: plusTen
- If you need to join two lists together use the “@” operator, let myList = plusTen @ newList, this should be used only when needed since it can be expensive depending on the size of the list. You cannot alter an element inside of the list natively, however you can write a function that would allow this to happen by copying the contents and creating a new list. A list is immutable
-
Operators
F# has the typical arithmetic operators, boolean operators, and comparison operators. One notable operator that is unique to F# is the compare operator.let x = 1 let y = 2 let lessThan compare x y //returns -1
let x = 1 let y = 2 let lessThan compare y x //returns 1
let x = 1 let y = 1 let lessThan compare y x //returns 0
-
Implicit type conversations do not happen automatically.
If you need to provide an explicit conversion for this addition to occur.let sum = 29 + 3.5 // does not compile
let sum = float 29 + 3.5 //works
-
There are a few built in implicit conversions in F# between types.
- sbyte converts to a signed byte
- byte converts to an unsigned byte
- int16 converts to a 16-bit signed integer
- uint16 converts to a 16-bit unsigned integer
- int or int32 converts to a 32 bit signed integer
- uint or uint32 converts to a 32 bit unsigned integer)
- int64 converts to a 64 bit signed integer
- uint64 converts to a 64 bit unsigned integer
- float converts to a double precision float
- float32 converts to a single precision float
- decimal converts to a decimal number
- string converts to a string
- char converts to a unicode character
- F# is a statically typed language, and type mismatches will be uncovered by the compiler.
- Type annotations are not required in F# like in C#. F# achieves this by using type interference to determine what that actual type is in the application. Because of this type interference it is possible that the compiler might choose the wrong type as a parameter or return value. This especially occurs when dealing with + and * calculations. If you are not sure you can easily add the type names to make sure your method has the proper signature of float instead of the potential inferred type of int.
- Functions are first class objects and can be passed around and even used a parameters in another functions.
- Being able to pass functions around is essential for predicates passed into List methods such as exists, filter, map, reduce, fold.
-
F# allows for function currying which allows you to create new functions based upon other functions.
What happens here is that you call function f with a parameter 3 it calls addn 7, addn then calls add n which then unravels to 3 + 7. I think I follow this, but not sure if I can describe it completely just yet.let add a b = a + b let addn n = add n let f = addn 7 // What is printed by the following line of code? printfn "Value of f 3 = %A" (f 3)
-
Namespaces and modules
To use another namespace in your F# program you use open System (similar to C#’s using System;)
Items accomplished
Read wikipediaordered a book