What the… F#A language you can brag about
Why you should care
Functional programming The new hotness
Next “big” .NET language CTP this summer
F# Background
OCaml offspring Created by MS Research Mixed mode language
Functional OO Imperative (if you must)
Statically typed
Variables… sort of
let x = 7let s = "kurt"let pi = 3.142
Yay! Type inference ...also, immutable So, really just
“values”
Lists are cool
let l = ['a';'b';'c';]// char list or list<char>
let digits = [0..9]// int list
Singly linked list Still immutable!
Sequences
let s = {1..10}// seq[1;2;3;...]
IEnumerable<T> Useful for
computing unbounded lists
Tuples – cliques for values
let me = ("kurt", 6.0)// string * float let point3d = (0,0,0)// int * int * int
Typed groups of values
Can “hold” anything
Basically awesome
Function types
let add x y = x + y// int -> int -> int
let eight = add 3 5// eight = 8
Like any other value
More type inference
Returns last expression
Even More Type Inference
let group x y = (x,y)// 'a -> 'b -> 'a * 'b
Look ma! Generics! Auto-generalization
Recursive Functions
let rec fib n = if n < 2 then n else (fib (n - 1)) + (fib (n - 2))
Explicitly recursive Tail recursion
Option types
type name = stringtype number = inttype date = System.DateTimetype meeting = | Personal of name * date | Phone of number * date let review = Personal("Jasmine",System.DateTime.Now)let call = Phone(8675309, System.DateTime.Now)
Pattern Matching
let what_to_do (m : meeting) = match m with | Personal(name,date) -> printfn "Meeting with %s at %A" name date | Phone(phone, date) -> printfn "Call %A at %A" phone date
The “fun” in functional
Functions are basic units of a program
We can mix them up in interesting ways Less OO design patterns!
“Cheap” functions are good Code is more expressive
Partial function application
let add3 = add 3// int -> int
let add x y = x + y// int -> int -> int
Pipelining operator
let (|>) x f = f x// 'a -> ('a -> 'b) -> 'b
let even x = x % 2 = 0// int -> boollet numbers = [1..20]// int listlet evens = numbers |> List.filter even// int list
Useful List functions
List.map// ('a -> 'b) -> // 'a list -> // 'b list[1..10] |> List.map add3// [4;5;6;..;13]
List.fold_left// ('b -> 'a -> 'b) ->// 'b ->// 'a list ->// 'b
[1..10] |> List.fold_left (fun acc x -> acc + x) 0// 55
Solving a problem
2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
What is the smallest number that is evenly divisible by all of the numbers from 1 to 20?
- Project Eulerprojecteuler.net
Imperative Programming
“Mutable” keyword For real variables
There are imperative loops Try not to use it too much
OO Programming
F# can be OO Classes Interfaces Inheritance
Cross language work “Hide” implementation
Creating a Class!
type OrderLine(n:string, q:int, p:float) = let mutable currName = n let mutable currQuantity = q let mutable currPrice = p new (name, price) = OrderLine(name, 1, price) member x.Name with get() = currName and set name = currName <- name member x.SubTotal with get() = (Float.of_int quantity) * price member x.OneMore() = currQuantity <- currQuantity + 1 currQuantity
When to do what
Why functional programming? Easy to build incrementally Simplified testing Concurrency!
Imperative can be good too At times, it’s wickedly fast It’s what we know
The end