Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (*
- 6.9
- A company consists of departments with sub-departments,
- which again can have sub-departments and so on.
- (The company can also be considered as a department.)
- 1. Assume that each department has a name and a (possibly)
- empty list of sub-departments Declare and F# type Department.
- 2 Extend this type so that each department has its own gross income.
- 3. Declare a function to extract a list of pairs
- (Department name, gross income), for all departments.
- 4. Declare a function to extract the total income for a given department
- by adding up its gross income, including the income of its sub-departments.
- 5. Declare a function to extract a list of pairs
- (Department name, total income) for all departments.
- 6. Declare a function format of type Department -> string, which can be used
- to get a textual form of a department such that names of sub-departments
- will occur suitably indented (e.g., with four spaces) on separate lines.
- (use printf to print out the results.
- Do not use printf in the declaration of format.)
- *)
- //1.1
- type DepartmentName = String
- type GrossIncome = Integer
- type Department = Dep of DepartmentName * Department List * GrossIncome
- //1.2 extension type Department with GrossIncome
- //1.3 -> map(DepartmentName*GrossIncome)
- let rec extractPairsAndGross :Department -> (DepartmentName*GrossIncome) List = function
- |(dName,dList,dGI):Department -> (dName,dGI)::List.foldBack(fun x list -> extractPairsAndGross(x)@list) dList []
- //1.4
- let rec totalIncome = function
- |(_,[],dGI) -> dGI //muligvis ligegyldig?
- |(dName,dList,dGI) -> List.foldBack(fun x list -> totalIncome(x) + list) dList []
- //1.5
- let rec extractPairsAndTotal = function
- |(dName,dList,dGI):Department -> (dName,totalIncome((dName,dList,dGI)))::List.foldBack(fun x list -> extractPairsAndTotal(x)@list) dList []
- //1.6
- let sysout :Department -> string = function
- |(dName,dList,_) -> printf(dName) //fuck ..
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement