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. [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. )

  1. [2 pts]

Give a recursive definition of the lookup function. (Part of the code is given; complete the following.)

lookup k [] =

lookup k =

  1. [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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!