Explore the fundamentals of Haskell’s type system with questions on type inference, type classes, and polymorphism. This quiz helps you solidify your understanding of how Haskell manages types, constraints, and generalized functions in functional programming.
Given the Haskell expression 'x = 5 + 3', what type will the compiler infer for x?
Explanation: The expression '5 + 3' involves integer addition, so the compiler infers the type as Int. String, Bool, and Char are not compatible with numeric operations in this context, making them incorrect.
Which Haskell keyword is used to define a type class?
Explanation: The keyword 'class' is used to define a type class in Haskell, such as 'class Eq a where ...'. The others are used for data type declarations, deriving instances, or organizing code and have different purposes.
In the function 'identity x = x', what is the inferred type of identity?
Explanation: The function 'identity' returns any type it is given, making it polymorphic; its type is 'a -u003E a'. 'Int -u003E Int', 'String -u003E Int', and 'Bool -u003E String' are specific types, missing the generalization shown here.
If you want to allow a custom data type to use the '==' operator, which type class should it be an instance of?
Explanation: 'Eq' is the type class responsible for equality operations like '=='. 'Ord' is for ordering, 'Show' for string representation, and 'Read' for parsing, making them less suitable here.
Why might you specify a type annotation in Haskell even though the compiler infers types?
Explanation: Type annotations improve code readability and can help the compiler resolve ambiguous types. They do not affect execution speed, file size, or module importing; those options are incorrect.
Which of these functions is polymorphic in Haskell?
Explanation: 'head' works with a list of any type, making it polymorphic. 'sum', 'not', and 'length' operate on specific types—integers, booleans, or characters—so they lack this generality.
In the type signature 'f :: (Eq a) =u003E a -u003E Bool', what does '(Eq a) =u003E' represent?
Explanation: '(Eq a) =u003E' means the type variable 'a' must be an instance of the 'Eq' type class. It is not related to function composition, module imports, or type synonyms, so those are incorrect.
What is achieved by writing 'data Color = Red | Blue deriving (Show)' in Haskell?
Explanation: 'deriving (Show)' creates default implementations for displaying 'Color'. It does not import modules, restrict possible values, or affect compile-time evaluation; those interpretations are incorrect.
What will be the inferred type of the expression '[True, False, True]' in Haskell?
Explanation: All elements are boolean values, so Haskell infers the type as '[Bool]'. '[Int]', '[Char]', and '[String]' do not match the boolean elements in the list, making those options incorrect.
How do type classes enable overloading of functions like '+' in Haskell?
Explanation: Type classes let different types define their own implementation of shared functions such as '+'. Modules manage organization, casting changes specific values, and static typing is about type checks rather than overloading.