Question: Note : Complete the following Haskell function definitions. Unless stated otherwise do not use library functions that are not in the Haskell standard prelude. Use

Note : Complete the following Haskell function definitions. Unless stated otherwise do not use library functions that are not in the Haskell standard prelude.

Use the specified function name as your code will be tested by a Haskell function expecting that function name. The testing program may use many more test cases than the ones shown in the specification. So, please test your functions extensively to ensure that you maximise your marks.

Q : 3 Write a function join :: Eq a => [(a,b)] -> [(a,c)] -> [(a,b,c)]. join takes two lists of pairs, and returns a single list of triples. A triple is generated only when there exists a member of both argument lists that have the same first element. The list elements are not sorted. This is the same semantics as the relational algebra natural join operation. For example: join [(2,"S"),(1,"J")] [(2,True),(3,False)] ? [(2,"S",True)] join [(2,"S"),(1,"J")] [(2,1),(2,2),(3,4)] ? [(2,"S",1),(2,"S",2)] Hint: use list a comprehension.

A) This question extends the join function from question 3. Write the function ljoin :: Eq a => [(a,b)] -> [(a,c)] -> [(a,b,Maybe c)]. This is the left outer join from relational algebra. If a tuple (database row) from the first (left) argument does not have a matching tuple from the second argument, include that tuple in the resulting tuple, but place a null value in place of the un-matched value. For this implementation we use a Maybe data type, and use the Nothing value to denote Null. For example ljoin [(2,"S"),(1,"J")] [(2,1),(2,2),(3,4)] ? [(2,"S",Just 1),(2,"S",Just 2),(1,"J",Nothing)]

Q: 3 is above.this question only for Q:(A) another question is not related to the Q : 3

B) Consider the following definition for a binary tree. data Tree a = Leaf a | Node (Tree a) (Tree a) A binary tree is balanced if, at every node, the difference between the number of leaves appearing in the left and right subtree is at most one. (A tree which contains just one leaf is considered balanced.) Write the function isBalanced :: Tree a -> Bool that decides if the tree is balanced. Hint: first write a function size::Tree a -> Int that counts leaves in a tree. isBalanced (Node (Node (Leaf 1)(Leaf 3)) (Leaf 2)) ? True isBalanced (Node (Node (Leaf 1)(Node (Leaf 1)(Leaf 3))) (Leaf 2)) ? False

C) Write a function isNumber :: String -> Bool that tests if a string contains a valid number. A valid number is defined in EBNF as: number ? .digit+ | digit+ [ .digit? ] For example, .5, 1.5, 1, 1. are all valid numbers. As usual, + signifies one or more occurrences, and * denotes zero or more. You may use the isDigit function from the Data.Char module. Hint: you may wish to write functions someDigits, manyDigits :: String -> Bool to test for .digit+ and digit? .

D) Write a function getElems :: [Int] -> [a] -> [Maybe a] which takes a list of integers signifying the position of an element in a list, and a list. It returns those elements that correspond to the positions specified. Because a position may be greater than the list size the returned element is a Maybe data type. If the position specified is greater the the maximum list position then Nothing is returned, else Just value. For example getElems [2,4] [1..10] ? [Just 3,Just 5] getElems [2,4] [1..4] ? [Just 3,Nothing]

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!