Advertisement
Guest User

assign2

a guest
Oct 23rd, 2014
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 3.71 KB | None | 0 0
  1. (*
  2.  
  3. 02157 Assignment 2
  4. Alexander Rosenberg Johansen
  5. s155706
  6.  
  7. *)
  8. //1.1
  9. type DepartmentName = string
  10. type GrossIncome = int
  11. type TotalIncome = int
  12. type Department = Dep of DepartmentName * Department List * GrossIncome
  13.  
  14. //Test cases for breadth and depth
  15. //Depth
  16. let d1 = Dep("SashaG", [], 100)
  17. let d2 = Dep("TorriB", [d1], 200)
  18. let d3 = Dep("FayeR", [d2], 300)
  19. //Breadth
  20. let d4 = Dep("KatjaK", [], 100)
  21. let d5 = Dep("BellaK", [], 100)
  22. let d6 = Dep("JenniferL",[],100)
  23. let dlist = [d4;d5;d6]
  24. let d8 = Dep("SkinD",dlist, 700)
  25.  
  26. //1.2 extension type Department with GrossIncome
  27. //No clue
  28.  
  29. //1.3 -> map(DepartmentName*GrossIncome)
  30. //1.3.1
  31. //Argument(s): Department, a recursive type corrisponding to a Department tree
  32. //Result(s): a tuple list with (DepartmentName,TotalIncome)
  33. //recursively going over every recursive subtype
  34. let rec ePaG :Department -> (DepartmentName*GrossIncome) List = function
  35.     |Dep(name,[],income)         -> [(name,income)]
  36.     |Dep(name,[x],income)        -> [(name,income)]@(ePaG x)
  37.     |Dep(name,dep::dList,income) -> (ePaG dep)@(ePaG (Dep(name,dList,income)))
  38.  
  39. //Test 1.3.1
  40. let t131d3 = ePaG d3
  41. let t131d8 = ePaG d8
  42.  
  43. //1.3.2 Just because
  44. let rec extractPairsAndGross :Department -> (DepartmentName*GrossIncome) List = function
  45.     |Dep(name,dList,income) -> (name,income)::(List.foldBack(fun x list -> extractPairsAndGross(x)@list) dList [])
  46.  
  47. //Test 1.3.2
  48. let t132d3 = extractPairsAndGross d3
  49. let t132d8 = extractPairsAndGross d8
  50.  
  51.  
  52.  
  53. //1.4
  54. //Argument(s): Department, a recursive type corrisponding to a Department tree
  55. //Result(s): The total income of the Department
  56. //Recursively add's the income of the department combined with recursively calling its subdepartments for adding GrossIncomes
  57. let rec totalIncome :Department -> TotalIncome = function
  58.     |Dep(name,dList,income) -> income + List.foldBack(fun x list -> totalIncome(x) + list) dList 0
  59.  
  60. //Test 1.4
  61. let t14d3 = totalIncome d3
  62. let t14d8 = totalIncome d8
  63.  
  64.  
  65. //1.5
  66. //Argument(s): Department, a recursive type corrisponding to a Department tree
  67. //Result(s): a tuple list with (DepartmentName,TotalIncome)
  68. //The same as 1.3, just with the totalIncome function as opposed to simply taken the income value.
  69. let rec extractPairsAndTotal :Department -> (DepartmentName * TotalIncome) list = function
  70.     |Dep(name,dList,income)    -> (name,totalIncome(Dep(name,dList,income)))::List.foldBack(fun x list -> extractPairsAndTotal(x)@list) dList []
  71.  
  72. //Test 1.5
  73. let t15d3 = extractPairsAndTotal d3
  74. let t15d8 = extractPairsAndTotal d8
  75.  
  76. //Argument(s): integer, responding to how many 'indentions' are required
  77. //Result(s): string, returns the amount of indentions in 4 x 'spaces'
  78. let rec lidtMereSnyd :int -> string = function
  79.     |i when i<=0    -> ""
  80.     |i when i>0     -> "    " + lidtMereSnyd (i-1)
  81.  
  82. //Argument(s): Department, responding to the departments
  83. //Result(s): string, with the departments indented
  84. //If you can do this without a counter, I will be amazed
  85. let rec lidtSnyd :Department*int -> string = function
  86.     |Dep(name,dList,_),i    -> name + List.foldBack(fun x list -> "\n" + lidtMereSnyd(i+1) + lidtSnyd(x,(i+1))+ list ) dList ""
  87.  
  88. //1.6 :Department -> string
  89. //Argument(s): Department, responding to the departments
  90. //Result(s): string, with the departments indented
  91. //In a cleaner representation, the foldback code would have been in lidtSnyd :Department -> string.
  92. //Thus having "format"'s 'only' function being to give the 'counter'
  93. let rec format :Department -> string = function
  94.     |Dep(name,dList,_)    -> name + List.foldBack(fun x list -> "\n    " + lidtSnyd(x,1) + list ) dList ""
  95.  
  96. //Test 1.6
  97. let t16d3 = "\n" + format d3
  98. let t16d8 = "\n" + format d8
  99.  
  100. printf "%s" t16d3
  101. printf "%s" t16d8
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement