act09ans

.pdf

School

Syracuse University *

*We aren’t endorsed by this school

Course

623

Subject

Computer Science

Date

Oct 30, 2023

Type

pdf

Pages

2

Uploaded by badMonk123

Report
Fall CIS Assured programming with formal methods Act. 09 Sol. Outline SU email: Class student number: Introduction In this activity, we will continue to work with the abstract data type Set (See the file Set. hs ) in Haskell (See Activity ). It implements a set (the mathematical structure) as a list without repetitions. The elements of the list is of type a where a is a member of the Eq type class (but not Ord ). By completing this activity successfully, you will be able to describe and explain an Haskell im- plementation of sets. In addition, you will be able to show how to write Haskell functions that utilize the given Set data type to manipulate data. Activity Suppose we define a new data type Set with a few support functions as follows: > newtype Set a = Set [a] > makeSet :: Eq a => [a] -> Set a > makeSet = Set . nub > f atten :: Set a -> [a] > f atten (Set xs) = xs > showSet :: (Eq a) => (a -> String) -> Set a -> String > showSet f (Set xs) = { ++ concat (map ((++ ) . f) (nub xs)) ++ } > union :: Eq a => Set a -> Set a -> Set a > union (Set xs) (Set ys) = Set (xs ++ ys) a ). Show how to define the set membership, set intersection and set difference operations, via list comprehension, for this Set data type. Answer > e em :: Eq a => a -> [a] -> Boo > e em x xs = or [ x == y | y <- xs] > memSet :: (Eq a) => a -> Set a -> Boo > memSet y (Set xs) = e em y xs > inter :: Eq a => Set a -> Set a -> Set a > inter (Set xs) (Set ys) = Set (int xs ys) > int :: Eq a => [a] -> [a] -> [a] > int xs ys = [ x | x <- xs, e em x ys] > diff :: Eq a => Set a -> Set a -> Set a > diff (Set xs) (Set ys) = Set (dif xs ys) > dif :: Eq a => [a] -> [a] -> [a] > dif xs ys = [ x | x <- xs, not (e em x ys)] Andrew C. Lee EECS Dept, Syracuse University
Fall CIS Assured programming with formal methods Act. 09 Sol. Outline b ). Given an input list s (of type [(a,b]) , define, by list comprehension, a function uniqueLst such that it is of the following type: uniqueLst :: (Eq a, b) => [(a, b)] -> Boo and, uniqueLst ts will return True if all tuples in the list s are distinct. Otherwise, it returns Fa se . Answer > uniqueLst :: (Eq a, Eq b) => [(a, b)] -> Boo > uniqueLst ts = and [ uniqueE (a,b) ts | (a,b) <- ts] uniqueE (x,y) ts verifies if the tup e (x,y) occurs in ts exact y once. > uniqueE :: (Eq a, Eq b) => (a,b) -> [(a, b)] -> Boo > uniqueE (x,y) ts = ( sum [ 1 | (x1,y1) <- ts , x1 == x, y1 == y] == 1) Andrew C. Lee EECS Dept, Syracuse University
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help

Browse Popular Homework Q&A

Q: However, while helium and krypton are very close to ideal gases, they are not exactly ideal.  What…
Q: Draw a vicinal alkyl bromide that would produce the following allene in an E. climination. Use a…
Q: (c) What is the relationship between the pressures at point A and point B? A and B are at the same…
Q: Using two equivalents of 1-bromobutane, propose a synthesis to make 4-octanone. Specify the reagents…
Q: Could you help fill in tables UV-13 to find the allele and genotype frequencies using the Hardy-…
Q: Suppose that we removed the line labeled #11, that is the call to Foo. __init_ from within Bar.…
Q: Muhammad is running on the road. Find the velocity as a function of displacement x, taking into…
Q: . Evaluate FdS where F = (x, y², z + y) and S is the boundary of the region contained in the…
Q: Determine the value of c such that the function is a probability mass function. g(x) = c√x for x =…
Q: inancial information related to Pegasus Products Company, a proprietorship, for the month ended…
Q: f(x, y) = 8xy - 2x² - y² + 3x - 9y +9 2. 9y+9 of дх af ду
Q: On a frictionless horizontal surface, a 9.46 kg block is pushed up against a 23,650 N/m spring. The…
Q: D Question 2 Saturated water vapor is at a temperature of 220 °C. What will be its specific volume…
Q: 2. The molecule LiH has a simple electronic structure. The H atom would like to gain an electron to…
Q: f(x, y) = 8xy - 2x² - y² + 3x - 9y +9 2. 9y+9 of дх af ду
Q: Phenylacetic acid (C6H₂CH₂CO₂H) is a weak monoprotic acid with K₂ = 4.90 x 10-5. What is the pH of a…
Q: Find a basis for the eigenspace corresponding to each listed eigenvalue of A below. - 1 0 - 3 A = -…
Q: Q2. Consult Paragraphs 3-6 of Quality Control Standard No. 20 (QC 20). Considering the example in…
Q: List and explain four times when there is an exception to the general rule that there is no…
Q: In each problem sketch the graph of the given function and express f(t) in terms of the unit step…
Q: Can you explain both of your answer?
Q: Given OZ with tangents XW and XY, what is mWVY ? (10a) 0 A. 70° O O O B. 140° C. 250° D. 270° (4a +…