Question
This is a Haskell question. Please provide the definitions for the functions in Haskell. And make sure there are no errors please. Here is the
This is a Haskell question. Please provide the definitions for the functions in Haskell. And make sure there are no errors please.
Here is the starter code provided (not everything is needed for this exercise):
module HW2types where
import Data.List (nub,sort)
-- Types for Exercise 1 -- type Bag a = [(a,Int)]
-- Types and functions for Exercise 2 -- type Node = Int type Edge = (Node,Node) type Graph = [Edge] type Path = [Node]
norm :: Ord a => [a] -> [a] norm = sort . nub
-- Types for Exercise 3 -- type Number = Int
type Point = (Number,Number) type Length = Number
data Shape = Pt Point | Circle Point Length | Rect Point Length Length deriving Show
type Figure = [Shape]
type BBox = (Point,Point)
Exercise 1. 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. typeBaga=[(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). [(5,1),(7,3),(2,1),(3,2),(8,1)] 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::EqaaBagaBaga (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 a single element from a multiset. Note that deleting 3 from {2,3,3,4} yields {2,3,4} whereas deleting 3 from {2,3,4} yields {2,4}. del::EqaaBagaBaga (c) Define a function bag that takes a list of values and produces a multiset representation. bag::Eqa[a]Baga For example, with xs=[7,3,8,7,3,2,7,5] we get the following result. >bagxs[(5,1),(7,3),(2,1),(3,2),(8,1)] (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::EqaBagaBagaBool 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 isset that tests whether a bag is actually a set, which is the case when each element occurs only once. isSet::EqaBagaBool (f) Define a function size that computes the number of elements contained in a bag. size::BagaInt
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started