In: Computer Science
Can someone show me how to write a Haskell function using snd on a list of tuples to create a new list of just the second element in each tuple? Ex. [(‘m’, False), (‘w’, False), (‘n’, True)] would evaluate to [False, False, True].
The program is given below. The comments are provided for the better understanding of the logic.
--The below function will return the 2nd element from a tuple.
--The input passsed to this element is a tuple of (Char, Bool) type
--The returned value is a Bool
--It uses the snd function to return the 2nd element from the tuple.
get2ndElement :: (Char, Bool) -> Bool
get2ndElement x = snd(x)
--The main function goes here
main = do
--declare the list of tuples with the input.
let list = [('m', False), ('w', False), ('n', True)]
--Use map and call the get2ndElement function by passing in the list
print(map get2ndElement list)
The screenshots of the code and output are provided below.