namespace System
Multiple items
type EntryPointAttribute =
  inherit Attribute
  new : unit -> EntryPointAttribute

Full name: Microsoft.FSharp.Core.EntryPointAttribute

--------------------
new : unit -> EntryPointAttribute
val main : argv:string [] -> int

Full name: index.main
val argv : string []
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
type Console =
  static member BackgroundColor : ConsoleColor with get, set
  static member Beep : unit -> unit + 1 overload
  static member BufferHeight : int with get, set
  static member BufferWidth : int with get, set
  static member CapsLock : bool
  static member Clear : unit -> unit
  static member CursorLeft : int with get, set
  static member CursorSize : int with get, set
  static member CursorTop : int with get, set
  static member CursorVisible : bool with get, set
  ...

Full name: System.Console
Console.ReadLine() : string
val ignore : value:'T -> unit

Full name: Microsoft.FSharp.Core.Operators.ignore
Multiple items
module Option

from Microsoft.FSharp.Core

--------------------
type Option<'a> =
  | Some of 'a
  | None

Full name: index.Option<_>
union case Option.Some: 'a -> Option<'a>
union case Option.None: Option<'a>
val validInt : Option<int>

Full name: index.validInt
val invalidInt : Option<'a>

Full name: index.invalidInt
val x : int

Introduction to F#

F# |> I <3

Grigoriy Belenkiy
Software developer, S&P Global Market Intelligence

Denver Dev Day
May 4, 2018

Imperative vs Functional

Imperative programming

  • Set of statements (sequence of actions)
  • Changes in program state (mutable, shared)
  • Describes how program operates

Functional programming

  • Evaluation of mathematical functions
  • Avoids mutable state and side-effects
  • Output value depends only on input

WTF#?

ML \(\rightarrow\) OCaml \(\rightarrow\) F#

Why F#?

  • Conciseness
  • Convenience
  • Correctness
  • Concurrency
  • Completeness

Hello World

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
// using namespace
open System

// entry point attribute for console app
[<EntryPoint>]
// main function
// note: no args type
let main argv =
  printfn "Hello World"
  // ignore function result
  // note: pipe operator - result from the
  // left is passed as the last parameter
  // to the next function
  Console.ReadLine() |> ignore
  // return value, i.e. exit code
  0

FizzBuzz

Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.

Factorial

\({\displaystyle n!= \prod_{k=1}^{n} k}\)

\(n! = \begin{cases} 1 & \quad \text{if } n = 0,\\ (n-1)! \times n & \quad \text{if }n > 0 \end{cases}\)

Tail Recursion

Tail Recursion

FizzBuzz (revisited)

Discriminated unions

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
type Option<'a> =       // use a generic definition  
   | Some of 'a         // valid value
   | None 

let validInt = Some 1
let invalidInt = None

match validInt with 
| Some x -> printfn "the valid value is %A" x
| None -> printfn "the value is None" 

Metric mishap caused loss of NASA orbiter

(CNN) -- NASA lost a $125 million Mars orbiter because a Lockheed Martin engineering team used English [imperial] units of measurement while the agency's team used the more conventional metric system for a key spacecraft operation, according to a review finding released Thursday.

CNN.com, September 30, 1999

Units of Measure

  • Float, signed int
  • Compile-time checking
  • Length, volume, mass, and so on... temperature?

Type providers

  • CSV
  • SQL
  • JSON
  • App.config

Resources

On-line

Books

Real-World Functional Programming With examples in F# and C#

Real-World Functional Programming With examples in F# and C# by Tomas Petricek with Jon Skeet

F# Deep Dives

F# Deep Dives edited by Tomas Petricek and Phillip Trelford

Expert F# 4.0

Expert F# 4.0 by Don Syme‎, Adam Granicz,‎ Antonio Cisternino

Mastering F#

Mastering F# by Alfonso Garcia-Caro Nunez,‎ Suhaib Fahad

Questions?

https://github.com/grishace/ddd-fsharp
https://github.com/grishace/ddd-fsharp

May the Fourth be with you!

1: 
                  printfn "Star" |>(+)<| "Wars"