Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Functional Programming Advanced

443 views

Published on

Published in: Technology, Sports
  • Be the first to comment

  • Be the first to like this

Functional Programming Advanced

  1. 1. FunctionalProgramming { F# Programming Techniques
  2. 2.  Oleksiy Tereshchenko  Principal consultant at Neudesic  10 years experience in Custom Application Development and Connected SystemsAbout Presenter
  3. 3.  Building Blocks  Tuples  Discriminated Unions  Lists  Stack (as example of OOD)  Recursive Functions  Assembling Parser  Demo & QuestionsAgenda
  4. 4.  Tuples  Discriminated Unions  Lists  Stack  Recursive FunctionsBuilding Blocks
  5. 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. 6. type Operation = Plus | Minus | Multiply | Devisiontype Value = IntValue of inttype ParseTree = Leaf of Value | Node of ParseTree * Operation * ParseTreelet plusOperation = Pluslet 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. 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.HeadLists
  8. 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. 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. 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
  11. 11. 5 * (3 + 4) = 35Assembling Parser(Calculator Parser)
  12. 12. Tokenize Expression Parse Sentence Print Tree Compute Expression Print ResultAssembling Parser(Flow Chart)
  13. 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 OperationAssembling Parser(Production Rules)

×