diff --git a/slides/03.2/03.2.FParsec.md b/slides/03.2/03.2.FParsec.md index f1f2f85..60171aa 100644 --- a/slides/03.2/03.2.FParsec.md +++ b/slides/03.2/03.2.FParsec.md @@ -7,11 +7,11 @@ ### Why FParsec -* Looks a lot like Parsec (from Haskell) -* Shows a very usefull library -* Demonstrate how some libraries look in FP (and F#) - * very different libraries in OOP -* Is building a DSL for parsing +* Looks a lot like Parsec (from Haskell)
+* Is building a DSL for parsing
+* Shows a very usefull library
+* Demonstrate how some libraries look in FP (and F#)
+ * properly different from anything you have seen --- @@ -60,11 +60,11 @@ type Commit = { Helper functions ```fsharp -let str_ws s = pstring s .>> spaces -let char_ws c = pchar c .>> spaces -let ws_char = spaces >>. pchar c +let str_ws s: Parser = pstring s .>> spaces +let char_ws c: Parser = pchar c .>> spaces +let ws_char c = spaces >>. pchar c let anyCharsTill pEnd = manyCharsTill anyChar pEnd -let line = anyCharsTill newline +let line<'u>: Parser = anyCharsTill newline let restOfLineAfter str = str_ws str >>. line ``` @@ -83,7 +83,8 @@ Parsing fields let id = restOfLineAfter "commit" let date = restOfLineAfter "Date:" -let email = ws_char '<' >>. anyCharsTill (char_ws '>') +let email<'u>: Parser = + ws_char '<' >>. anyCharsTill (char_ws '>') let name = anyCharsTill (lookAhead email) let author = str_ws "Author:" >>. name .>>. email ``` diff --git a/slides/03.2/index.html b/slides/03.2/index.html index 736e235..60cb263 100644 --- a/slides/03.2/index.html +++ b/slides/03.2/index.html @@ -13,6 +13,23 @@ +
diff --git a/slides/04/immutable-data-structures.md b/slides/04/immutable-data-structures.md index 974d543..6c9ab49 100644 --- a/slides/04/immutable-data-structures.md +++ b/slides/04/immutable-data-structures.md @@ -23,20 +23,20 @@ ## What -* Mutating operations +* Mutating operations
* should not change structure - * instead opy -* Can share data between 'versions' + * instead copy +* Can share data between 'versions'
---- ## Why -* Pros: - * immutable is easier to reason about +* Pros:
+ * immutablility makes reasoning easier * automatically thread safe * easier to implement :) -* Cons: +* Cons:
* slower? * uses more memory * harder to implement :) @@ -59,6 +59,8 @@ module BST = // Code for creating/adding etc. ``` +---- + ##### Invariants * An element `'a` in a Node * is **greater** than all elements in left sub-tree @@ -72,17 +74,16 @@ We could also use .fsi files to declare functions ### Operations ```fsharp -type empty<'a> = unit -> BST<'a> -type insert<'a> = 'a -> BST<'a> -> BST<'a> -type remove<'a> = 'a -> BST<'a> -> BST<'a> -type contains<'a> = 'a -> BST<'a> -> bool +module BST = + val empty<'a> : unit -> BST<'a> + val insert<'a> : 'a -> BST<'a> -> BST<'a> + val remove<'a> : 'a -> BST<'a> -> BST<'a> + val contains<'a> : 'a -> BST<'a> -> bool ``` Could be extended with -* `map` -* `filter` -* `fold` -* etc. + +`map`, `filter`, `fold`, etc. note: @@ -108,9 +109,9 @@ let rec contains (x: 'a) (n: BST<'a>) = * Algorithm: ![BST](./img/bst.png "Binary search tree") - 1. Use a variation of `contains` to find correct place to insert - 2. Copy nodes as we move down through the tree -* Example + 1. Use a variation of 'contains' to locate correct place to insert
+ 2. Copy nodes as we move down through the tree
+* Example
* Inserting 9 ---- @@ -131,10 +132,10 @@ let rec contains (x: 'a) (n: BST<'a>) = ![BST](./img/insert3.png "Binary search tree") -* **Notice:** -* Only copy a small part
of the tree - * How much? -* Does it effect run-time
for `insert` +* **Notice:**
+* Only copy a small part
of the tree
+* How much?
+* Does it effect run-time
for 'insert'?
@@ -143,14 +144,14 @@ let rec contains (x: 'a) (n: BST<'a>) = ## `Set<'a>` -* From .NET F# lib -* **Invariant:** Elements are unique -* Finite -* Only elements where `ordering` is defined -* Internally represented as a `BBT<'a>` -* Immutable +* From .NET F# library +* Invariant: Elements are unique
+* Finite
+* Immutable
* All operations returns a new `set` -* Mathmatical set operations available +* Mathmatical set operations available
+* Only elements where 'ordering' is defined
+* Internally represented as a 'BBT<'a>'
Note: BBT: balanced binary three @@ -205,21 +206,21 @@ Set.difference first third ## Sets in general -* `map`, `filter`, `fold`, `foldBack` are all `$ O(n) $` -* complexity of recusion is in worst case `$ O(n*log(n)) $` -* Enumarations can be used to simplify and optimize this +* map, filter, `fold`, foldBack are all $ O(n) $
+* complexity of recusion is in worst case $ O(n*log(n)) $
+* Enumarations can be used to simplify and optimize this --- ## `Map<'a>` * From .NET F# lib -* **Invariant:** Keys are unique +* Invariant: Keys are unique * Key / value pair * Lookup per key -* Immutable -* Implemented using `BBT<'a>` -* As Set, Map requires ordering is defined for key type +* Immutable
+* Implemented using 'BBT<'a>'
+* As Set, Map requires ordering is defined for key type Note: BBT: balanced binary three @@ -256,9 +257,9 @@ exists: ('Key - 'T -> bool) -> Map<'Key, 'T> -> bool ---- -### fold and foldBack on Maps +### `fold` and `foldBack` on Maps -Definition for fold, foldBack on map takes key/value pair into account +Uses key/value pair in functions ```fsharp fold: ('State -> 'Key -> 'T -> 'State) @@ -279,8 +280,8 @@ Map.fold (fun s k v -> s + v) 0 m ## Equality -* `=` operator is defined for both `list`, `set` and `map` -* Equal is consists of same elements (structural) +* = operator is defined for both 'list', 'set' and 'map'
+* Equal means same elements (structural)
* For `list` order of elements matters ```fsharp @@ -296,6 +297,7 @@ set [1;2] = set [2;1] set [1;2] = set [3;2] // val it : bool = false ``` +
--- @@ -305,10 +307,10 @@ set [1;2] = set [3;2] ---- -### Lazy +### Sequences -* Sequences in F# are lazy -* Possibly infinite +* Sequences in F# are lazy
+* Possibly infinite
```fsharp // Create finite @@ -317,6 +319,7 @@ seq [1;2;3;4;5] let nat = Seq.initInfinite (fun i -> i) // [f 0; f 1; f 2; ...] ``` +
---- @@ -327,20 +330,20 @@ Seq.item 5 nat // val it: int 5 ``` -* `Seq.item 5 nat` evaluates the 5th element, but not elements `0-4` and `6->...` -* Calling `Seq.item 5 nat` will evaluate the 5th element again +* 'Seq.item 5 nat' evaluates the 5th element, but not elements '0-4' and '6-...' +*
Calling 'Seq.item 5 nat' will evaluate the 5th element again ---- -### Cache +### Caching sequences ```fsharp // Seq.cache: seq<'a> -> seq<'a> let cachedNat = Seq.cache nat ``` -* Calling `Seq.item 5 cachedNat` will evaluate all elements from 0-5 -* Calling again will not evalute elements 0-5 again +* Calling 'Seq.item 5 cachedNat' will evaluate all elements from '0-5' +* Calling again will not evalute elements '0-5' again --- diff --git a/slides/04/index.html b/slides/04/index.html index 859746e..36a1a8e 100644 --- a/slides/04/index.html +++ b/slides/04/index.html @@ -17,6 +17,23 @@ +