2. Oleksiy Tereshchenko
Principal consultant at Neudesic
10 years experience in Custom Application
Development and Connected Systems
About Presenter
3. Building Blocks
Tuples
Discriminated Unions
Lists
Stack (as example of OOD)
Recursive Functions
Assembling Parser
Demo
& Questions
Agenda
5. let makeAndYear = ("Ford", 2011)
type CarMakeAndYear = string * int
let (make,year) : CarMakeAndYear = makeAndYear
Debug.WriteLine(make)
Debug.WriteLine(year)
match makeAndYear with
| ("Honda", 2010) -> Debug.WriteLine("Matched Honda 2010")
| ("Honda", year) -> Debug.WriteLine("Matched Honda " +
year.ToString())
| ( _ , _ ) -> Debug.WriteLine("This is not Honda")
Tuples
6. type Operation = Plus | Minus | Multiply | Devision
type Value = IntValue of int
type ParseTree = Leaf of Value | Node of ParseTree * Operation * ParseTree
let plusOperation = Plus
let treeLeafLeft = Leaf(IntValue(100))
let treeLeafRight = Leaf(IntValue(100))
let treeNode = Node(treeLeafLeft, Plus, treeLeafRight)
let parentTree = Node(treeNode, Multiply, Leaf(IntValue(2)))
Discriminated Unions
7. let listOfNumbersA = [1;2;3]
let listOfNumbersB = [1..10]
let listOfNumbersAB = listOfNumbersA @ listOfNumbersB
let listOfNumbersC = 50 :: listOfNumbersA
match listOfNumbersA with
|intValue :: listOfNumbersD -> Debug.WriteLine(intValue)
|_-> ()
let head = listOfNumbersB.Head
Lists
8. type StackNode<'a> = Nil | StackNode of 'a * StackNode<'a>
type Stack<'a>() =
let mutable stackNode : StackNode<'a> = Nil
member self.Node : StackNode<'a> = stackNode
member self.Push( leaf : 'a ) =
stackNode <- StackNode(leaf, self.Node)
member self.Pop() : 'a option =
match stackNode with
| Nil ->
None
| StackNode(leaf,tailNode) ->
stackNode <- tailNode; Some(leaf);
Stack
9. let rec SumArithmeticProgression( x) =
if x < 1 then
x
else
x + SumArithmeticProgression ( (x-1))
let rec SumArithmeticProgressionTailRecursion ( x, acc) =
if x < 1 then
acc
else
SumArithmeticProgressionTailRecursion ( (x-1), (acc + x))
Recursive Functions
10. let PrintToken (token : Token) =
match token with
| Operation(operation) -> PrintOperation(operation)
| Bracket(LeftBracket) -> Debug.Write "("
| Bracket(RightBracket) -> Debug.Write ")"
| Letter(Value(value)) -> PrintValue value
| Letter(ParseTree(parseTree)) -> PrintTree parseTree
;;
let rec PrintSentence (sentence : Token List) =
match sentence with
| token :: tail -> PrintToken(token); PrintSentence(tail);
| [] -> ()
;;
Recursive Functions
12. Tokenize
Expression
Parse Sentence
Print Tree
Compute
Expression
Print Result
Assembling Parser
(Flow Chart)
13. L =V
L =T
L ao L = L ao L mo L
L =LoL
L =(L)
L – Letter
V – Value
T – Tree
o – Operation
ao – Additive Operation
mo – Multiplicative Operation
Assembling Parser
(Production Rules)