Question: please write code in Haskell and do (c) We can represent a two dimensional (sparse) array of characters in Haskell as a list of pairs


We can represent a two dimensional (sparse) array of characters in Haskell as a list of pairs of the form ((r,c),char) where r and c are integers representing the row and column where char appears. If a row-column pair does not appear, then we assume that the entry is a space character, ".For simplicity, we assume that the entries in the list are in row-major order. This means that each row is listed from left to right, one row after the other. Thus the 3 by 3 grid below: Ixl lol | Ixlol II Ix would be represented by the list: (0,0), 'x'), ((0,2), 'o'), ((1,1), 'x'),((1,2). 'o').((2,2), "x")] (c) Please write a function addItem to insert a new character into a list in the form above. It should have the following type: addItem :: (Num a, Num ai, Ord a, Ord ab) => a -> a1 -> (a, a1) -> t -> (((a, ai), t)] -> [((a, al), t)] You should assume that the list is in row-major order and ensure that the resulting list is also in row-major order. If the entry is already in the list, you should update the value, while if it is not in the list, you should add it. The function should take as arguments the number of rows, number of columns, the row-column pair where the character is to be placed, the character to be placed, and the current representation of the array, If arep is the list above then addItem 33 (1,0) 'o' arep should result in the list [(0,0),'x'), ((0,2), '0'),((1,0),'o').((1,1), 'x'), ((1,2),'o'), ((2, 2),'*')] while addItem 33 (1,1) 'o arep should result in the list [((0,0), 'x').(0,2), '0'),((1,1), 'o'), ((1,2),'o'), ((2,2), 'x')] If the row and column given for the new entry are outside the limits of the array, then the array representation should be returned unchanged. Thus evaluating addItem 3 3 (4,1) 'a' arep and addItem 3 3 (-1,1) 'a' arep should both just return arep. You may assume that the number of rows and number of columns given are both positive. (I.e., your program need not check to make sure they are both positive.) Make sure your function has the same functionality as that given in the examples. L.e., make sure it can be applied to the arguments in the examples, written in exactly the same way! You will find it helpful to use the functions defined above
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
