Computers and Technology

Programming with Lists Multisets, or bags, can be represented as list of pairs (x, n) where n indicates the number of occurrences of x in the multiset. type Bag a - [(a, Int)] For the following exercises you can assume the following properties of the bag representation. But note: Your function definitions have to maintain these properties for any multiset they produce! (1) Each element x occurs in at most one pair in the list. (2) Each element that occurs in a pair has a positive counter. As an example consider the multiset {2, 3, 3,5,7,7,7,8), which has the following representation (among others) Note that the order of elements is not fixed. In particular, we cannot assume that the elements are sorted. Thus the above list representation is just one example of several possible. (a) Define the function ins that inserts an element into a multiset. ins :: Eq a => a-> Bag a-> Bag a (Note: The class constraint "Eq a =>" restricts the element type a to those types that allow the comparison of elements for equality with --.) (b) Define the function del that removes an element from a multiset. del Eq a -> Bag a Bag a (c) Define a function bag that takes a list of values and produces a multiset representation bag :: Eq a => [a] -> Bag a For example, with xs7,3,8,7,3,2,7,5] we get the following result. > bag xs (Note: It's a good idea to use of the function ins defined earlier.) (d) Define a function subbag that determines whether or not its first argument bag is contained in the second. subbag Eq aBag a Bag aBool Note that a bag b is contained in a bag b' if every element that occurs n times in b occurs also at least n times in b'. (e) Define a function isbag that computes the intersection of two multisets. isbag Eq Bag a ->Bag a -Bag a (0) Define a function size that computes the number of elements contained in a bag. sizeBag a ->Int

answer
Answers: 2

Other questions on the subject: Computers and Technology

image
Computers and Technology, 21.06.2019 22:00, savannahckatz
Business professionals of america, and future business leaders of america – phi beta lambda are both open to business students at which levels? check all that apply. elementary school middle school high school college
Answers: 1
image
Computers and Technology, 23.06.2019 06:20, kiarakagni
What is a point-in-time measurement of system performance?
Answers: 3
image
Computers and Technology, 23.06.2019 19:30, Felixthecat7186
Anul 2017 tocmai s-a încheiat, suntem trişti deoarece era număr prim, însă avem şi o veste bună, anul 2018 este produs de două numere prime, 2 şi 1009. dorel, un adevărat colecţionar de numere prime, şi-a pus întrebarea: “câte numere dintr-un interval [a, b] se pot scrie ca produs de două numere prime? “.
Answers: 3
image
Computers and Technology, 24.06.2019 07:30, Isabellawuzhere
Aproject involves many computing systems working together on disjointed task towards a single goal what form of computing would the project be using
Answers: 3
Do you know the correct answer?
Programming with Lists Multisets, or bags, can be represented as list of pairs (x, n) where n indica...

Questions in other subjects:

Konu
Mathematics, 30.11.2021 01:00
Konu
History, 30.11.2021 01:00