Haskell Programming -- 1. Fill in the gaps. Complete the following definitions.
ID: 3749253 • Letter: H
Question
Haskell Programming
-- 1. Fill in the gaps. Complete the following definitions. It does not
-- matter what the definitions do as long as they are type correct. For e1, e2,
-- e5, and e7, this means uncommenting the type declaration and replacing the ? with
-- type(s) correspond to the definitions directly below them. For e3, e4,
-- e6, and e8, this means replacing the undefined with an expression of appropriate type.
--
-- Note: you should complete each definition one at a time.
--e1 :: ?
e1 = [True,False,True]
--e2 :: ?
e2 = [[1,2],[3,4]]
e3 :: (Char,Bool)
e3 = undefined
e4 :: [(String,Int)]
e4 = undefined
--e5 :: ? -> ?
e5 n = n*2
e6 :: Int -> Int -> Int
e6 = undefined
--e7 :: ? -> ?
e7 (x,y) = (y,x)
e8 :: a -> (a,a)
e8 = undefined
Explanation / Answer
e1 :: [Bool]
e1 = [True,False,True]
e2 :: Num t => [[t]]
e2 = [[1,2],[3,4]]
e3 :: (Char,Bool)
e3 = ('a',True)
e4 :: [(String,Int)]
e4 = [("hello",10),("world",20)]
e5 :: Num a => a -> a
e5 n = n*2
e6 :: Int -> Int -> Int
e6 a b = a+b
e7 :: (t1, t) -> (t, t1)
e7 (x,y) = (y,x)
e8 :: a -> (a,a)
e8 x = (x,x)
Explaination:
1. e1 is a list of Bool
2.e2 is a list of list of Num
3.for e3 we are declaring a return type of the form (Char,Bool)
4.for e4 we are declaring a return type of the form (String,Int)
5.e5 takes a Num and returns square of it.
6.for e6 we can define any function that takes Int and Int as input and returns an Int
7.we are swapping x and y so the types also get swapped
8.e8 just repeat some element x in a tuple
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.