Upgrade to Pro — share decks privately, control downloads, hide ads and more …

F# 101

F# 101

Short introduction to F#.
1. https://github.com/mat3u/kata-fsharp-template - Empty F# solution with NUnit and FAKE
2. https://github.com/MSledzinski/args-parser-fskata - Kata Args Solution (by Marek Śledziński)
3. https://gist.github.com/mat3u/d864d371fe168db36ffe - Sample Game Of Life implementation

Links:

1. http://fsharpforfunandprofit.com
2. http://fsharp.org

Matt Stasch

March 03, 2015
Tweet

More Decks by Matt Stasch

Other Decks in Programming

Transcript

  1. F# is a mature, open source, cross-platform, functional-first programming language.

    It empowers users and organizations to tackle complex computing problems with simple, maintainable and robust code.
  2. Immutability & Data Types let a = 5 a =

    6 // Error let mutable b = 5 b <- 6 let p = („str”, 5) type Book = { Title: string; Author: Author } let lotr = {Title = „Lord of the Rings”; Author = „J.R.R. Tolkien”} let silm = {lotr with Title = „Silmarilion”}
  3. Higher-order function let squares = List.map (fun n -> n

    * n) [1 .. 100] let squares = [1 .. 100] |> List.map (fun n -> n * n)
  4. Higher-order function let squares = List.map (fun n -> n

    * n) [1 .. 100] let double a = a + a let doubles = [1 .. 100] |> List.map double let squares = [1 .. 100] |> List.map (fun n -> n * n)
  5. Currying let sum a b = a + b (int,

    int) -> int int -> int -> int
  6. λ Calculus let f = fun x -> fun y

    -> x + y let g x y = x + y
  7. Partial application let sum a b = a + b

    let inc = sum 1 inc 5 // 6
  8. let rec factorial n = match n with | 0

    -> 1 | n -> (factorial (n - 1)) * n Pattern Matching
  9. let rec inc list = match list with | []

    -> 1 | x::xs -> (x + 1)::(inc xs) Pattern Matching cont.
  10. let abs a = match a with | a when

    a < 0 -> a * -1 | a -> a Pattern Matching cont.
  11. let x::xs = [1;2;3] let (x,y) = point let (x,_)

    = point Pattern Matching cont.
  12. let rec factorial n = match n with | 0

    -> 1 | n -> (factorial (n - 1)) * n Reccurency & tail call optimization
  13. let factorial n = let rec factorial’ n acc match

    n with | 0 -> acc | n -> factorial’ (n - 1) (n * acc) factorial’ n 1 Reccurency & tail call optimization
  14. let factorial n = let rec factorial’ n acc match

    n with | 0 -> acc | n -> factorial’ (n - 1) (n * acc) factorial’ n 1 Reccurency & tail call optimization
  15. open NUnit.Framework open Game [<Test>] let ``Should die if alone``

    () = let world = [Cell (0, 0)] let next = step world Assert.IsTrue(next.IsEmpty) Smooth integration with .NET Framework
  16. 1. Any live cell with fewer than two live neighbours

    dies, as if caused by underpopulation. 2. Any live cell with more than three live neighbours dies, as if by overcrowding. 3. Any live cell with two or three live neighbours lives on to the next generation. 4. Any dead cell with exactly three live neighbours becomes a live cell.
  17. Hints Pattern matching for list: match my_list with | []

    -> … | x::tail -> … | x::y::tail -> … Records and creating new using previous: type Book = { Title: string; Author: Author } let lotr = {Title = „Lord of the Rings”; Author = „J.R.R. Tolkien”} let silm = {lotr with Title = „Silmarilion”} Tail call optimization :)