let it be

Introduction to F#
Kit Eason - @kitlovesfsharp – www.kiteason.com – [email protected]
F# in a nutshell
 Microsoft first class supported language for .NET
 Supports OO and functional paradigms
 Compiles to CLI like C#, VB.Net
 First class citizen in VS2010 through VS2013
 Open source, runs on Mono
 Strongly typed
let it be
 Use ‘let’ to declare functions and values
let a = 1
let Add x y = x + y
let CircleArea r = System.Math.PI * r ** 2.
Point of no return (-statements)
 The return value of the function is the last value calculated (no ‘return’ statement)
let StrToDoubleDefault str def =
let ok, result = System.Double.TryParse str
if ok then
result
else
def
Yeah, it’s magic
 Types are inferred – at design time
The bonfire of the parentheses
 Lexical scope is determined by indentation
 Argument lists don’t have brackets (normally)
let StrToDoubleDefault str def =
let ok, result = System.Double.TryParse str
if ok then
result
else
def
|> operator makes you :)
 “Forward pipe”
 Takes output from previous operation…
 …and feeds it as input to the next
let AbsSquareRoot (n : double) =
n |> abs |> sqrt
// sqrt(abs(n))
F# Interactive (FSI)
 Use F# Interactive to define and try out functions
> let add x y =
x + y;;
val add : x:int -> y:int -> int
> add 3 4;;
val it : int = 7
>
Array module
 Provides operations that work on whole arrays
 Similar to LINQ
let CircleArea r = System.Math.PI * r ** 2.
let circles = [|3.; 5.2; 9.9|]
let TotalArea radii =
radii
|> Array.sumBy (fun r -> CircleArea r)
Array.filter
 Another operation from the Array module
 Returns an array with just the elements that pass the test
let transactions = [|310.99; -52.80; 99.99; -128.30; 58.25|]
let TotalCredits txns =
txns
|> Array.filter (fun t -> t > 0.)
|> Array.sum