Answered step by step
Verified Expert Solution
Question
1 Approved Answer
4. One common way to store an information table is with an association list, which is a list of pairs. Each pair (k,v) represents an
4. One common way to store an information table is with an association list, which is a list of pairs. Each pair (k,v) represents an entry in the table, where k is the lookup-key and v is the value stored for key k. The expecta- tion is that each possible key shows up in at most one pair in the list, but the same value may be associated with multiple different keys. For example, the inventory for an extremely small fruit stand could be represented by an association list as follows inventory [(String, Int)] inventory C(" apples,5), "bananas",7), ("peaches",1), ("oranges",8), "plums",7), ("pears",5), ("mangoes",6), ("papayas",10) 1 In this case, the fruit stand has 7 bananas and 6 mangoes in stock. For the purposes of this assignment, we'll consider only those association lists whose pairs have type (String, Int) (such as inventory above). For each of the functions below, you should assume that the input list is an association list (i.e., no key appears in more than one pair) (a) Use recursion on lists to write a function total: [(String, Int) Int such that total alist calculates the sum of all of the integers stored in alist. For example, total inventory should return 49 (b) Use recursion on lists to write a function tooFew Int -> [(String, Int)) >String] such that tooFew num alist returns a list of all the keys in alist whose current value is less than or equal to num. For example, tooFew 6 inventory should return C"apples", "peaches", "pears", "mangoes"] (c) Use recursion on lists to write a function restock : String Int -> [(String, Int)] -> (String, Int) 4. One common way to store an information table is with an association list, which is a list of pairs. Each pair (k,v) represents an entry in the table, where k is the lookup-key and v is the value stored for key k. The expecta- tion is that each possible key shows up in at most one pair in the list, but the same value may be associated with multiple different keys. For example, the inventory for an extremely small fruit stand could be represented by an association list as follows inventory [(String, Int)] inventory C(" apples,5), "bananas",7), ("peaches",1), ("oranges",8), "plums",7), ("pears",5), ("mangoes",6), ("papayas",10) 1 In this case, the fruit stand has 7 bananas and 6 mangoes in stock. For the purposes of this assignment, we'll consider only those association lists whose pairs have type (String, Int) (such as inventory above). For each of the functions below, you should assume that the input list is an association list (i.e., no key appears in more than one pair) (a) Use recursion on lists to write a function total: [(String, Int) Int such that total alist calculates the sum of all of the integers stored in alist. For example, total inventory should return 49 (b) Use recursion on lists to write a function tooFew Int -> [(String, Int)) >String] such that tooFew num alist returns a list of all the keys in alist whose current value is less than or equal to num. For example, tooFew 6 inventory should return C"apples", "peaches", "pears", "mangoes"] (c) Use recursion on lists to write a function restock : String Int -> [(String, Int)] -> (String, Int)
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