Question
8) [5 pts] Do in Haskell Please A Haskell list of tuples can be used to represent a lookup table where the first value of
8) [5 pts] Do in Haskell Please
A Haskell list of tuples can be used to represent a lookup table where the first value of each pair is the key and the second value is the associated value.
For example: [("a",1),("b",2),("c",3),("a",4)]
(Assume the keys in a lookup table are not necessarily unique.)
We want to define a Haskell function (lookup k table) that returns the values for key k in the lookup table. If key k doesnt appear in the table, it should return an empty list.
Thus,
lookup 3 [(1,"4"),(2,"5"),(1,"3")] returns []
lookup 1 [(1,"4"),(2,"5"),(1,"3")] returns ["4","3"]
lookup "b" [("a",1),("b",2),("c",3),("a",4)] returns [2]
- [1 pt]
What should be the Haskell type of the lookup function? (Note: The input list is not necessarily (Int,String) or (String,Int) tuples. )
- [2 pts]
Give a recursive definition of the lookup function. (Part of the code is given; complete the following.)
lookup k [] =
lookup k =
- [2 pts]
The following isKey function is a predicate function that checks whether a given value v is the key in the given pair.
isKey v (x,y) = (v==x)
For example: (isKey 1 (1,"4")) returns True
Now re-define lookup function using isKey, map, and filter functions. Your solution should not use explicit recursion and instead be combinator-based. You may define additional non-recursive helper function(s) if needed.
lookup v iL =
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