Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

bartleby

Concept explainers

Question

Sets are collections (1) without defined order and (2) not allowing duplication. Multisets, also called “bags” are collections without defined order but which permit duplication, i.e., more than one element. We define the function #(a B) to be the number of occurrences of the element a in the bag B. For example, #(1, [1 1 2 3 4 4 5]) is 2 and #(5, [1 1 2 3 4 4 5]) = 1. Bag union and intersection are defined in terms of #. 

bag-union: List × List -> List
This function should take as arguments two lists representing bags and should return the list representing their bag-union.


bag-intersection : List × List -> List
This function should take as arguments two lists representing bags and should return the list representing their bag-intersection. 

Allowed functions. Your code must use only the following functions:
1. define, let
2. lambda, curry
3. cons, car, cdr, list, list?, append, empty?, length, equal?
4. and, or, not
5. if, cond
6. map, append-map, andmap, ormap, filter, apply
7. +, -, /, *

Racket code only please. Thank you!

Expert Solution
Check Mark
Step 1

Here's the implementation of bag-union and bag-intersection in Racket:

(define (bag-union bag1 bag2)
  (let ((merged (append bag1 bag2)))
    (filter (lambda (x) (not (zero? (apply + (map (lambda (y) (if (equal? x y) 1 0)) merged))))) merged)))

(define (bag-intersection bag1 bag2)
  (let ((merged (append bag1 bag2)))
    (filter (lambda (x) (not (zero? (min (apply + (map (lambda (y) (if (equal? x y) 1 0)) bag1)) (apply + (map (lambda (y) (if (equal? x y) 1 0)) bag2))))))) merged)))

 

 

this is the implementation of bag-union and bag-intersection in Racket as described in my previous answer. The bag-union function first appends the two bags bag1 and bag2 to form a merged list, and then uses the filter function to remove duplicates. The filter function takes as input a function (lambda expression) that checks if the number of occurrences of an element in the merged list is greater than 0. The bag-intersection function works in a similar manner, but instead of filtering elements that have a non-zero count in the merged list, it filters elements that have non-zero counts in both bag1 and bag2.

Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education