------------------------------------------------------------------------------ Mohamed G. Gouda CS 337 Fall 2007 Midterm 3 ------------------------------------------------------------------------------ Open Book and Notes 60 Minutes ------------------------------------------------------------------------------ 1. Specify a Haskell (second-order) function "largerf" that takes three parameters, namely two (first-order) functions f and g and a list xs, and returns a boolean result. In order to compute the boolean result, the two functions f and g are first applied to each element in list xs. Then the boolean result is True iff the result of applying f to each element x in xs is larger than or equal to the result of applying g to the same element x in xs. ------------------------------------------------------------------------------ 2. Specify a Haskell function "lessp" that takes two parameters, namely (w,x) and (y,z), and returns a boolean result. The returned result is True iff either w is smaller than y, or w equals y and x is smaller than or equal to z. What will the Haskell interpreter return to the command Main>:t lessp ------------------------------------------------------------------------------ 3. Use function "lessp" in Problem 2 to specify a Haskell function "insertp" that takes two parameters, namely a 2-tuple (w,x) and a sorted list zs of 2-tuples, and returns the sorted list that results from inserting the 2-tuple (w,x) into list zs. What will the Haskell interpreter return to the command Main>:t insertp ------------------------------------------------------------------------------ 4. Consider a database that has the following two relations: Student (name, address, email) Grade (name, major, gpa) Specify a predicate p that makes the following assertion hold Sel.p(Student Join Grade) = Sel.p(Student) Join Sel.p(Grade) Specify a predicate p that makes the following assertion hold Sel.p(Student Join Grade) != Sel.p(Student) Join Sel.p(Grade) ------------------------------------------------------------------------------ 5. Consider a database that has two compatible relations R(a,b) and S(a,b). Each of the two relations has only two tuples, and the domain of values for each of the two attributes a and b is the set {0,1}. Specify relations R and S such that the following assertion hold Prj.a(R Intersect S) = Prj.a(R) Intersect Prj.a(S) Specify relations R and S such that the following assertion hold Prj.a(R Intersect S) != Prj.a(R) Intersect Prj.a(S) ------------------------------------------------------------------------------ 6. Consider a database that has the following two relations: Student (id, name, email, major) Grade (id, grade1, grade2, ..., grade100, gpa) Write an optimized query to compute the average gpa for the students whose major is cs. ------------------------------------------------------------------------------- Solution ------------------------------------------------------------------------------- 1. largerf f g [] = True largerf f g (x:xs) | (f x) >= (g x) = largerf f g xs | True = False ------------------------------------------------------------------------------- 2. lessp (w,x) (y,z) = (w :t lessp lessp :: (Ord a, Ord b) => (b,a) -> (b,a) -> Bool ------------------------------------------------------------------------------ 3. insertp (w,x) [] = [(w,x)] insertp (w,x) ((y,z):zs) | lessp (w,x) (y,z) = (w,x):((y,z):zs) | True = (y,z):(insertp (w,x) zs) Main> :t insertp insertp :: (Ord a, Ord b) => (a,b) -> [(a,b)] -> [(a,b)] ------------------------------------------------------------------------------ 4. p names only attributes that are in both Student and Grade; for example p can be (name = "john") p names attributes that are either not in Student or not in Grade; for example p can be (name = "john" ^ address = "xyz") ------------------------------------------------------------------------------ 5. R (a, b) S(a, b) 0 0 0 0 0 1 1 0 R (a, b) S(a, b) 0 0 0 0 1 1 1 0 ------------------------------------------------------------------------------- 6. The optimized query is A.avg gpa(Sel.(major=cs)(Prj.(id, major) (Student) Join Prj.(id, gpa) (Grade))) -------------------------------------------------------------------------------