F# Presentation
-
Upload
mrkurt -
Category
Technology
-
view
1.643 -
download
2
description
Transcript of F# Presentation
![Page 1: F# Presentation](https://reader033.fdocuments.in/reader033/viewer/2022061212/5495bfd8b479597e6a8b60af/html5/thumbnails/1.jpg)
What the… F#A language you can brag about
![Page 2: F# Presentation](https://reader033.fdocuments.in/reader033/viewer/2022061212/5495bfd8b479597e6a8b60af/html5/thumbnails/2.jpg)
Why you should care
Functional programming The new hotness
Next “big” .NET language CTP this summer
![Page 3: F# Presentation](https://reader033.fdocuments.in/reader033/viewer/2022061212/5495bfd8b479597e6a8b60af/html5/thumbnails/3.jpg)
F# Background
OCaml offspring Created by MS Research Mixed mode language
Functional OO Imperative (if you must)
Statically typed
![Page 4: F# Presentation](https://reader033.fdocuments.in/reader033/viewer/2022061212/5495bfd8b479597e6a8b60af/html5/thumbnails/4.jpg)
Variables… sort of
let x = 7let s = "kurt"let pi = 3.142
Yay! Type inference ...also, immutable So, really just
“values”
![Page 5: F# Presentation](https://reader033.fdocuments.in/reader033/viewer/2022061212/5495bfd8b479597e6a8b60af/html5/thumbnails/5.jpg)
Lists are cool
let l = ['a';'b';'c';]// char list or list<char>
let digits = [0..9]// int list
Singly linked list Still immutable!
![Page 6: F# Presentation](https://reader033.fdocuments.in/reader033/viewer/2022061212/5495bfd8b479597e6a8b60af/html5/thumbnails/6.jpg)
Sequences
let s = {1..10}// seq[1;2;3;...]
IEnumerable<T> Useful for
computing unbounded lists
![Page 7: F# Presentation](https://reader033.fdocuments.in/reader033/viewer/2022061212/5495bfd8b479597e6a8b60af/html5/thumbnails/7.jpg)
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
![Page 8: F# Presentation](https://reader033.fdocuments.in/reader033/viewer/2022061212/5495bfd8b479597e6a8b60af/html5/thumbnails/8.jpg)
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
![Page 9: F# Presentation](https://reader033.fdocuments.in/reader033/viewer/2022061212/5495bfd8b479597e6a8b60af/html5/thumbnails/9.jpg)
Even More Type Inference
let group x y = (x,y)// 'a -> 'b -> 'a * 'b
Look ma! Generics! Auto-generalization
![Page 10: F# Presentation](https://reader033.fdocuments.in/reader033/viewer/2022061212/5495bfd8b479597e6a8b60af/html5/thumbnails/10.jpg)
Recursive Functions
let rec fib n = if n < 2 then n else (fib (n - 1)) + (fib (n - 2))
Explicitly recursive Tail recursion
![Page 11: F# Presentation](https://reader033.fdocuments.in/reader033/viewer/2022061212/5495bfd8b479597e6a8b60af/html5/thumbnails/11.jpg)
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)
![Page 12: F# Presentation](https://reader033.fdocuments.in/reader033/viewer/2022061212/5495bfd8b479597e6a8b60af/html5/thumbnails/12.jpg)
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
![Page 13: F# Presentation](https://reader033.fdocuments.in/reader033/viewer/2022061212/5495bfd8b479597e6a8b60af/html5/thumbnails/13.jpg)
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
![Page 14: F# Presentation](https://reader033.fdocuments.in/reader033/viewer/2022061212/5495bfd8b479597e6a8b60af/html5/thumbnails/14.jpg)
Partial function application
let add3 = add 3// int -> int
let add x y = x + y// int -> int -> int
![Page 15: F# Presentation](https://reader033.fdocuments.in/reader033/viewer/2022061212/5495bfd8b479597e6a8b60af/html5/thumbnails/15.jpg)
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
![Page 16: F# Presentation](https://reader033.fdocuments.in/reader033/viewer/2022061212/5495bfd8b479597e6a8b60af/html5/thumbnails/16.jpg)
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
![Page 17: F# Presentation](https://reader033.fdocuments.in/reader033/viewer/2022061212/5495bfd8b479597e6a8b60af/html5/thumbnails/17.jpg)
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
![Page 18: F# Presentation](https://reader033.fdocuments.in/reader033/viewer/2022061212/5495bfd8b479597e6a8b60af/html5/thumbnails/18.jpg)
Imperative Programming
“Mutable” keyword For real variables
There are imperative loops Try not to use it too much
![Page 19: F# Presentation](https://reader033.fdocuments.in/reader033/viewer/2022061212/5495bfd8b479597e6a8b60af/html5/thumbnails/19.jpg)
OO Programming
F# can be OO Classes Interfaces Inheritance
Cross language work “Hide” implementation
![Page 20: F# Presentation](https://reader033.fdocuments.in/reader033/viewer/2022061212/5495bfd8b479597e6a8b60af/html5/thumbnails/20.jpg)
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
![Page 21: F# Presentation](https://reader033.fdocuments.in/reader033/viewer/2022061212/5495bfd8b479597e6a8b60af/html5/thumbnails/21.jpg)
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
![Page 22: F# Presentation](https://reader033.fdocuments.in/reader033/viewer/2022061212/5495bfd8b479597e6a8b60af/html5/thumbnails/22.jpg)
The end