Advertisement
Guest User

noWin.fs

a guest
Oct 21st, 2014
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 1.97 KB | None | 0 0
  1. (*
  2. 6.9
  3. A company consists of departments with sub-departments,
  4. which again can have sub-departments and so on.
  5. (The company can also be considered as a department.)
  6.  
  7. 1.  Assume that each department has a name and a (possibly)
  8.     empty list of sub-departments Declare and F# type Department.
  9. 2   Extend this type so that each department has its own gross income.
  10. 3.  Declare a function to extract a list of pairs
  11.     (Department name, gross income), for all departments.
  12. 4.  Declare a function to extract the total income for a given department
  13.     by adding up its gross income, including the income of its sub-departments.
  14. 5.  Declare a function to extract a list of pairs
  15.     (Department name, total income) for all departments.
  16. 6.  Declare a function format of type Department -> string, which can be used
  17.     to get a textual form of a department such that names of sub-departments
  18.     will occur suitably indented (e.g., with four spaces) on separate lines.
  19.     (use printf to print out the results.
  20.     Do not use printf in the declaration of format.)
  21.  
  22. *)
  23. //1.1
  24. type DepartmentName = String
  25. type GrossIncome = Integer
  26. type Department = Dep of DepartmentName * Department List * GrossIncome
  27. //1.2 extension type Department with GrossIncome
  28.  
  29. //1.3 -> map(DepartmentName*GrossIncome)
  30.  
  31. let rec extractPairsAndGross :Department -> (DepartmentName*GrossIncome) List = function
  32.     |(dName,dList,dGI):Department -> (dName,dGI)::List.foldBack(fun x list -> extractPairsAndGross(x)@list) dList []
  33.  
  34. //1.4
  35. let rec totalIncome = function
  36.     |(_,[],dGI)         -> dGI //muligvis ligegyldig?
  37.     |(dName,dList,dGI)  -> List.foldBack(fun x list -> totalIncome(x) + list) dList []
  38.  
  39. //1.5
  40. let rec extractPairsAndTotal = function
  41.     |(dName,dList,dGI):Department    -> (dName,totalIncome((dName,dList,dGI)))::List.foldBack(fun x list -> extractPairsAndTotal(x)@list) dList []
  42.  
  43. //1.6
  44. let sysout :Department -> string = function
  45.     |(dName,dList,_)    -> printf(dName) //fuck ..
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement