F#

Aggelos Biboudis

@biboudis

15/1/2015

Functional Programming

  • Application of functions
  • Functions are first-class citizens
  • Avoid state and mutation
  • Call the function twice and get the same answer
  • Expressions, not statements

History

  • Alonzo Church - Lambda calculus (1930)
  • John McCarthy - Lisp (1958)
  • Robin Milner - ML (1970)
  • Gérard Huet - Caml (1985)
  • Xavier Leroy - OCaml (1996)
  • Don Syme - F# (2005)

F#

  • a statically typed programming language
    • functional
    • object oriented
    • imperative
    • explorative
  • for .NET (anywhere)
  • with open source compiler

Functions

> let add (a:int) (b:int) = a + b;;

val add : a:int -> b:int -> int

> let add a b = a + b;;

val add : a:int -> b:int -> int

> add 3;;

val it : (int -> int) = <fun:it@35>

> let rec fib n = if n < 2 then 1 else fib (n - 1) + fib (n - 2);;

val fib : n:int -> int

> let apply f a b = (f a b);;

val apply : f:('a -> 'b -> 'c) -> a:'a -> b:'b -> 'c

> apply add 3 4;;

val it : int = 7

composition and pipelining

> let function1 x = x + 1;;

val function1 : x:int -> int

> let function2 x = x * 2;;

val function2 : x:int -> int

> let h = function1 >> function2;;

val h : (int -> int)

> (>>);;

val it : (('a -> 'b) -> ('b -> 'c) -> 'a -> 'c) = <fun:it@10>

> let result = 100 |> function1 |> function2;;

val result : int = 202

lambdas

> apply (fun a b -> a + b) 3 4;;

val it : int = 7

Datatypes (tuples, records)

> let tuple = (1, "two");; // tuples

val tuple : int * string = (1, "two")

> type record = { Id : int; Name : string };; //records

type record =
  {Id: int;
   Name: string;}

> let customer = { Id = 3 ; Name = "Aggelos"};;

val customer : record = {Id = 3;
                         Name = "Aggelos";}

Datatypes (discriminated unions)

> type switchstate =
    | On
    | Off;;

type switchstate =
  | On
  | Off

> let x = On;;

val x : switchstate = On

> type switchstate =
    | On
    | Off
    | Adjustable of float;;

type switchstate =
  | On
  | Off
  | Adjustable of float

> let z = Adjustable(0.25);;

val z : switchstate = Adjustable 0.25

Datatypes (Lists)

> let numbers = [1; 2; 3; 4; 5; 6; 7; 8; 9; 10];;
val numbers : int list
 
> numbers;;
val it : int list = [1; 2; 3; 4; 5; 6; 7; 8; 9; 10]

> 1 :: 2 :: 3 :: [];;
val it : int list = [1; 2; 3]

> [1 .. 10];;
val it : int list = [1; 2; 3; 4; 5; 6; 7; 8; 9; 10]

> List.rev [1 .. 5];;
val it : int list = [5; 4; 3; 2; 1]

> [1 .. 10] |> List.filter (fun x -> x % 2 = 0);;
val it : int list = [2; 4; 6; 8; 10]

> [1 .. 10] |> List.map (fun x -> (x * x).ToString());;
val it : string list
= ["1"; "4"; "9"; "16"; "25"; "36"; "49"; "64"; "81"; "100"]

pattern matching

> let greeting name =
    match name with
    | "Aggelos" | "George" -> "Hello!"
    | "Maria" -> "Hola!"
    | _ -> "DOES NOT COMPUTE!";;

val greeting : name:string -> string

> let rec printList list =
    match list with
    | x :: xs -> printf "%d " x; printList xs
    | [] -> printfn "";;

val printList : xs:int list -> unit

> let list1 = [ 1; 5; 100; 450; 788 ];;

val list1 : int list = [1; 5; 100; 450; 788]

> printList list1;;

1 5 100 450 788 

val it : unit = () 

IMPerative programming

> let mutable x = 5;;
 
val mutable x : int
 
> x;;
val it : int = 5
 
> x <- 10;;
val it : unit = ()
 
> x;;
val it : int = 10

object oriented programming

> type Account(number : int, holder : string) = class
    let mutable amount = 0m
 
    member x.Number = number
    member x.Holder = holder
    member x.Amount = amount
 
    member x.Deposit(value) = amount <- amount + value
    member x.Withdraw(value) = amount <- amount - value
end

type Account =
  class
    new : number:int * holder:string -> Account
    member Deposit : value:decimal -> unit
    member Withdraw : value:decimal -> unit
    member Amount : decimal
    member Holder : string
    member Number : int
  end

object oriented programming

> let bob = new Account(123456, "Bob’s Savings");;

val bob : Account

> bob.Deposit(100M);;
val it : unit = ()

> bob.Withdraw(29.95M);;
val it : unit = ()

more oop features

  • inheritance
  • interfaces
  • operator overloading
  • events
  • namespaces
  • modules

Thank you

Resources at http://fsharp.org/

 

Code / Structure inspired by the F# Programming Wikibook and the F# Language Reference.

F#

By Aggelos Biboudis