Advertisement
Guest User

Untitled

a guest
Jan 19th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 7.09 KB | None | 0 0
  1. (*
  2.  
  3.    ITT8060 -- Advanced Programming 2016
  4.    Department of Computer Science
  5.    Tallinn University of Technology
  6.    ------------------------------------
  7.  
  8.    Coursework 3: User defined types
  9.  
  10.    ------------------------------------
  11.    Name: Taavi Gilden
  12.    TUT Student ID: a155835
  13.    ------------------------------------
  14.  
  15.  
  16.    Answer the questions below.  You answers to questions 1--7 should be
  17.    correct F# code written after the question. This file is an F#
  18.    script file, it should be possible to load the whole file at
  19.    once. If you can't then you have introduced a syntax error
  20.    somewhere.
  21.  
  22.    This coursework will be graded. It has to be submitted to the TUT
  23.    git system using the instructions on the course web page by October 9, 2015.
  24.  *)
  25.  
  26.  // 1. Consider expression trees of type ExprTree declared in the lecture.
  27.  // Extend the type with if-then-else expression of the form:
  28.  // if b then e1 else e2
  29.  // where b is a boolean expression and e1 and e2 are expressions.
  30.  // An example expression could be:
  31.  
  32.  type ExpressionTree =
  33.      | Const of int
  34.      | Ident of string
  35.      | Minus of ExpressionTree
  36.      | Sum of ExpressionTree * ExpressionTree
  37.      | Diff of ExpressionTree * ExpressionTree
  38.      | Prod of ExpressionTree * ExpressionTree
  39.      | Let of string * ExpressionTree * ExpressionTree
  40.      | BiggerThan of ExpressionTree * ExpressionTree
  41.      | LesserThan of ExpressionTree * ExpressionTree
  42.      | BiggerEq of ExpressionTree * ExpressionTree
  43.      | LesserEq of ExpressionTree * ExpressionTree
  44.      | Equal of ExpressionTree * ExpressionTree
  45.      | And of ExpressionTree * ExpressionTree
  46.      | Or of ExpressionTree * ExpressionTree
  47.      | Not of ExpressionTree
  48.      | Logical of ExpressionTree * ExpressionTree * ExpressionTree
  49.      | Match of ExpressionTree
  50. -    
  51. +
  52. +(*
  53. +  FEEDBACK:
  54. +
  55. +    The Match alternative is wrong.
  56. +*)
  57. +
  58.  // match p with [p1, ex1; p2,ex2 ...]
  59.  // match "a" with ["a", 1; "b", 2]
  60.  let expr2 = Match(Logical(Equal(Ident("a"), Ident("a")),Const(1),Logical(Equal(Ident("a"),Ident("b")),Const(2), Const(0))))
  61.  
  62.  // if a+3 > b+c && a>0 then c+d else e
  63.  let expr1 = Logical(And(BiggerThan(Sum(Ident("a"), Const(3)),Sum(Ident("b"),Ident("c"))), BiggerThan(Ident("a"), Const(0))), Sum(Ident("c"),Ident("d")),Ident("e"))
  64.  
  65.  // a* (-3 + let x = 5 in x + a)
  66.  let expr = Prod(Ident "a", Sum( Minus(Const(3)), Let("x", Const(5), Sum(Ident "x", Ident "a"))))
  67.  
  68.  // 2. Extend the function eval defined in the lecture to support the
  69.  // if-then-else expressions defined in Q1.
  70.  
  71.  // eval ExprTree -> map<string, int> -> int
  72.  let rec eval t env =
  73.      match t with
  74.      | Const n -> n
  75.      | Ident s -> Map.find s env
  76.      | Minus t -> - (eval t env)
  77.      | Sum (t1, t2) -> eval t1 env + eval t2 env
  78.      | Diff (t1, t2) -> eval t1 env - eval t2 env
  79.      | Prod (t1, t2) -> eval t1 env * eval t2 env
  80.      | Let (s, t1, t2) ->    
  81.              let v1 = eval t1 env
  82.              let env1 = Map.add s v1 env
  83.              eval t2 env1
  84.      | Logical (t1, t2, t3) ->
  85.          if (eval t1 env) = 1
  86.          then eval t2 env
  87.          else eval t3 env
  88.      | BiggerThan (t1, t2) ->
  89.          if eval t1 env > eval t2 env
  90.          then 1
  91.          else 0
  92.      | LesserThan (t1, t2) ->
  93.          if eval t1 env < eval t2 env
  94.          then 1
  95.          else 0
  96.      | BiggerEq (t1, t2) ->
  97.          if eval t1 env >= eval t2 env
  98.          then 1
  99.          else 0
  100.      | LesserEq (t1, t2) ->
  101.          if eval t1 env <= eval t2 env
  102.          then 1
  103.          else 0
  104.      | Equal (t1, t2) ->
  105.          if eval t1 env = eval t2 env
  106.          then 1
  107.          else 0
  108.      | And (t1, t2) ->
  109.          if (eval t1 env + eval t2 env) = 2
  110.          then 1
  111.          else 0
  112.      | Or (t1, t2) ->
  113.          if (eval t1 env + eval t2 env) >= 1
  114.          then 1
  115.          else 0
  116.      | Not (t1) ->
  117.          if eval t1 env = 1
  118.          then 0
  119.          else 1
  120.      | Match (t1) ->
  121.          eval t1 env
  122.  
  123. +(*
  124. +  FEEDBACK:
  125. +
  126. +    OK
  127. +*)
  128.  
  129.  let map1 = Map.ofList[("a",1);("b",2);("c",3);("d",4);("e",5)]
  130.  
  131.  eval expr1 map1
  132.  eval expr2 map1
  133.  // 3-4: Given the type definition:
  134.  type BList =
  135.    | BEmpty
  136.    | Snoc of BList * int
  137.  
  138.  // 3. Make the function filterB: (prop: int -> bool) BList -> BList that will return a list for the elements of which
  139.  // the function prop returns true.
  140.  
  141.  let rec filterB (prop: int -> bool) (input: BList): BList =
  142.      match input with
  143.      | BEmpty -> BEmpty
  144.      | Snoc(head, tail) ->
  145.          let filtered = filterB prop head
  146.          if prop tail
  147.          then Snoc (filtered, tail)
  148.          else filtered
  149.  
  150. +(*
  151. +  FEEDBACK:
  152. +
  153. +    OK
  154. +*)
  155. +
  156.  let testList = Snoc(Snoc(BEmpty,3),2)
  157.  let func = (>) 3
  158.  filterB func testList
  159.  
  160.  // 4. Make the function mapB: (trans: int -> int) BList -> BList that will return a list where the function trans has
  161.  // been applied to each element.
  162.  
  163.  let rec mapB (trans: int -> int) (input: BList): BList =
  164.      match input with
  165.      | BEmpty -> BEmpty
  166.      | Snoc (head, tail) -> Snoc(mapB trans head, trans tail)
  167.  
  168. +(*
  169. +  FEEDBACK:
  170. +
  171. +    OK
  172. +*)
  173. +
  174.  let addOne = (+) 1
  175.  mapB addOne testList
  176.  
  177.  // 5-7. Given the type definition
  178.  type Tree =
  179.    | Nil
  180.    | Branch2 of Tree * int * Tree
  181.    | Branch3 of Tree * int * Tree * int * Tree
  182.  //
  183.  // 5. Define the value exampleTree : Tree that represents the following
  184.  //    tree:
  185.  //
  186.  //        2
  187.  //       / \
  188.  //      *  3 5
  189.  //        / | \
  190.  //       *  *  *
  191.  
  192.  let exampleTree: Tree = Branch2(Nil, 2, Branch3(Nil, 3, Nil, 5, Nil))
  193.  
  194. +(*
  195. +  FEEDBACK:
  196. +
  197. +    OK
  198. +*)
  199. +
  200.  // 6. Define a function sumTree : Tree -> int that computes the sum of
  201.  //    all labels in the given tree.
  202.  
  203.  let rec sumTree (tree: Tree): int =
  204.      match tree with
  205.      | Nil -> 0
  206.      | Branch2(tree1, int1, tree2) -> sumTree tree1 + int1 + sumTree tree2
  207.      | Branch3(tree1, int1, tree2, int2, tree3) -> sumTree tree1 + int1 + sumTree tree2 + int2 + sumTree tree3
  208.  
  209. +(*
  210. +  FEEDBACK:
  211. +
  212. +    OK
  213. +*)
  214. +
  215.  sumTree exampleTree
  216.  
  217.  // 7. Define a function productTree : Tree -> int that computes the
  218.  //    product of all labels in the given tree. If this function
  219.  //    encounters a label 0, it shall not look at any further labels, but
  220.  //    return 0 right away.
  221.  
  222.  let rec productTree (tree: Tree): int =
  223.      match tree with
  224.      | Nil -> 1
  225.      | Branch2(tree1, int1, tree2) ->
  226.          if int1 = 0
  227.          then 0
  228.          else productTree tree1 * int1 * productTree tree2    
  229.      | Branch3(tree1, int1, tree2, int2, tree3) ->
  230.          if (int1 = 0 || int2 = 0)
  231.          then 0
  232.          else productTree tree1 * int1 * productTree tree2 * int2 * productTree tree3
  233.  
  234. +(*
  235. +  FEEDBACK:
  236. +
  237. +    While your implementation does not look at the subtrees if it encounters a
  238. +    labelĀ 0, it still looks at all other remaining parts of the tree, which it
  239. +    should not do.
  240. +*)
  241. +
  242.  let exampleTree2 = Nil
  243.  productTree exampleTree
  244.  
  245.  // ** Bonus questions **
  246.  
  247.  // 8. Extend the ExprTree type with a pattern match expression
  248.  // match p with [p1, ex1; p2,ex2 ...]
  249.  
  250.  // 9. Extend the eval function to support match expressions.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement