exception notImplemented (* ------------------------------------------------------------*) (* QUESTION 1 : Partial Sums *) (* ------------------------------------------------------------*) (* Given a list of integers, compute the partial sums i.e. the sums of all the prefixes psum: int list -> int list *) fun psum L = raise notImplemented (* Some test cases - psum [1,1,1,1,1]; val it = [1,2,3,4,5] : int list - psum [1,2,3,4]; val it = [1,3,6,10] : int list - psum []; val it = [] : int list - psum [9]; val it = [9] : int list *) (* ------------------------------------------------------------*) (* QUESTION 3 : Mobile *) (* ------------------------------------------------------------*) datatype mobile = Object of int | Wire of mobile * mobile (* A object is represented by its weight (= an integer) and a wire is represented by the two mobiles attached to its ends *) (* ------------------------------------------------------------*) (* Q3.1: Weight *) (* ------------------------------------------------------------*) (* val weight: mobile -> int weight(m) = total weight of the mobile m *) fun weight M = raise notImplemented (* ------------------------------------------------------------*) (* Q3.2: Balance *) (* ------------------------------------------------------------*) (* val balanced : mobile -> bool balanced (m) ==> true, if weight of m's left end = weight of m's right end; and each of the sub-parts are also balanced. false otherwise Note: it is not simply enough to check that two children have the same weight == it still could mean that a sub-tree is unbalanced. *) fun balanced M = raise notImplemented (* ------------------------------------------------------------*) (* Q3.3: Reflection *) (* ------------------------------------------------------------*) (* We can reflect a mobile about its verical axis: for an object, the reflection is just itself; for a wire, we swap the positions of the two mobiles hanging off its end. Reflection is applied recursively on the subparts. *) (* val reflect: mobile -> mobile reflect(m) => a mobile that is the complete reflection of m *) fun reflect m = raise notImplemented (* ------------------------------------------------------------*) (* Q3.4 and Q3.5: Redundant Mobile *) (* ------------------------------------------------------------*) (* We modify the representation of the mobile slightly and keep the weight information at the wire. The weight at the wire is the sum of the weight of each mobiles attached to it. *) datatype rmobile = RObj of int | RWire of rmobile * int * rmobile (* val rweight: rmobile -> int constant time function which computes the total weight of an rmobile *) fun rweight m = raise notImplemented (* val consistent: rmobile -> bool consistent(m) ==> true if at every RWire the weight information is the sum of the weight of each mobiles attached otherwise false. *) fun consistent m = raise notImplemented