Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (*
- 02157 Assignment 2
- Alexander Rosenberg Johansen
- s155706
- *)
- //1.1
- type DepartmentName = string
- type GrossIncome = int
- type TotalIncome = int
- type Department = Dep of DepartmentName * Department List * GrossIncome
- //Test cases for breadth and depth
- //Depth
- let d1 = Dep("SashaG", [], 100)
- let d2 = Dep("TorriB", [d1], 200)
- let d3 = Dep("FayeR", [d2], 300)
- //Breadth
- let d4 = Dep("KatjaK", [], 100)
- let d5 = Dep("BellaK", [], 100)
- let d6 = Dep("JenniferL",[],100)
- let dlist = [d4;d5;d6]
- let d8 = Dep("SkinD",dlist, 700)
- //1.2 extension type Department with GrossIncome
- //No clue
- //1.3 -> map(DepartmentName*GrossIncome)
- //1.3.1
- //Argument(s): Department, a recursive type corrisponding to a Department tree
- //Result(s): a tuple list with (DepartmentName,TotalIncome)
- //recursively going over every recursive subtype
- let rec ePaG :Department -> (DepartmentName*GrossIncome) List = function
- |Dep(name,[],income) -> [(name,income)]
- |Dep(name,[x],income) -> [(name,income)]@(ePaG x)
- |Dep(name,dep::dList,income) -> (ePaG dep)@(ePaG (Dep(name,dList,income)))
- //Test 1.3.1
- let t131d3 = ePaG d3
- let t131d8 = ePaG d8
- //1.3.2 Just because
- let rec extractPairsAndGross :Department -> (DepartmentName*GrossIncome) List = function
- |Dep(name,dList,income) -> (name,income)::(List.foldBack(fun x list -> extractPairsAndGross(x)@list) dList [])
- //Test 1.3.2
- let t132d3 = extractPairsAndGross d3
- let t132d8 = extractPairsAndGross d8
- //1.4
- //Argument(s): Department, a recursive type corrisponding to a Department tree
- //Result(s): The total income of the Department
- //Recursively add's the income of the department combined with recursively calling its subdepartments for adding GrossIncomes
- let rec totalIncome :Department -> TotalIncome = function
- |Dep(name,dList,income) -> income + List.foldBack(fun x list -> totalIncome(x) + list) dList 0
- //Test 1.4
- let t14d3 = totalIncome d3
- let t14d8 = totalIncome d8
- //1.5
- //Argument(s): Department, a recursive type corrisponding to a Department tree
- //Result(s): a tuple list with (DepartmentName,TotalIncome)
- //The same as 1.3, just with the totalIncome function as opposed to simply taken the income value.
- let rec extractPairsAndTotal :Department -> (DepartmentName * TotalIncome) list = function
- |Dep(name,dList,income) -> (name,totalIncome(Dep(name,dList,income)))::List.foldBack(fun x list -> extractPairsAndTotal(x)@list) dList []
- //Test 1.5
- let t15d3 = extractPairsAndTotal d3
- let t15d8 = extractPairsAndTotal d8
- //Argument(s): integer, responding to how many 'indentions' are required
- //Result(s): string, returns the amount of indentions in 4 x 'spaces'
- let rec lidtMereSnyd :int -> string = function
- |i when i<=0 -> ""
- |i when i>0 -> " " + lidtMereSnyd (i-1)
- //Argument(s): Department, responding to the departments
- //Result(s): string, with the departments indented
- //If you can do this without a counter, I will be amazed
- let rec lidtSnyd :Department*int -> string = function
- |Dep(name,dList,_),i -> name + List.foldBack(fun x list -> "\n" + lidtMereSnyd(i+1) + lidtSnyd(x,(i+1))+ list ) dList ""
- //1.6 :Department -> string
- //Argument(s): Department, responding to the departments
- //Result(s): string, with the departments indented
- //In a cleaner representation, the foldback code would have been in lidtSnyd :Department -> string.
- //Thus having "format"'s 'only' function being to give the 'counter'
- let rec format :Department -> string = function
- |Dep(name,dList,_) -> name + List.foldBack(fun x list -> "\n " + lidtSnyd(x,1) + list ) dList ""
- //Test 1.6
- let t16d3 = "\n" + format d3
- let t16d8 = "\n" + format d8
- printf "%s" t16d3
- printf "%s" t16d8
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement