60 likes | 193 Views
Lab1 – Interesting lessons learned. Modeling Real world modeled onto the language at hand: Shunting station as a record: state(main:_ one:_ two:_) Wagons as atoms: a, b, c etc. Tracks as lists Etcetera…. Lab1 – Interesting lessons learned. Recursion {ApplyRules Ms}
E N D
Lab1 – Interesting lessons learned • Modeling • Real world modeled onto the language at hand: • Shunting station as a record: state(main:_ one:_ two:_) • Wagons as atoms: a, b, c etc. • Tracks as lists • Etcetera… ali@sics.se
Lab1 – Interesting lessons learned • Recursion • {ApplyRules Ms} • Make sure the recursion terminates. We knew ApplyRules terminated since it was called with a smaller list in each recursive call • Different solutions had different efficiency. {ApplyRule one(N1+N2)|Mr} vs. one(N1+N2) |{ApplyRule Mr} ali@sics.se
Lab1 – Interesting lessons learned • Decompose problems into small functions • {SplitTrain Xs Y} simplified substantially • ApplyMoves was one of the tougher functions: fun {ApplyMoves S Ms} case Ms of nil then … [] M|Mr then S1 = case M of one(N) then if …then …else … end [] two(N) then if … then … else … end end in … end end • Could have been split into ApplyMove and ApplyMoves? Easier to write? ApplyMove could be reused in FindMove? ali@sics.se
Lab2 – Interesting lessons learned • Abstract Data Types • Do not break the abstraction! I.e. do not directly access the underlying data in the tree, i.e. Tree.Frekvens, instead use your ADT {Byte Tree} • MakeTest can test your compression algorithm even though it doesn’t know how you implemented it. • TreeView can show you a visual representation of the tree and its contents regardless of your implementation. ali@sics.se
Lab2 – Interesting lessons learned • Complexity • Carefully consider functions that your recursive functions are calling. • Using Append in HUF slows down recursive functions, particularly since the first argument is a large list. • In Traverse we call Append since we assume that our bit sequence for each symbol is relatively short! • Make your functions tail-recursive so that they use constant stack-space • If the function calls itself on several locations ali@sics.se
Lab2 – Interesting lessons learned • Try to derive state-invariants • Use accumulator variables which are updated during the recursion and when the base case is true the accumulator holds the final result. • E.g. FrequencyMap and Bitmap/Traverse • Higher order programming simplify matters. Recursion can be avoided. • E.g. InitLeaves canuse Map to generate a list of leaves from a list of frequencies. ali@sics.se