07. HC7 ‐ Haskell Chapter 7 Practical Tasks: Intro‐to‐Type‐Classes - wimsio/universities GitHub Wiki
Eq
instance for a custom data type
HC7T1: Implement an Define a data type Color
representing Red, Green, and Blue. Implement the Eq
type class for it so that colors of the same type are considered equal.
Ord
instance for a custom data type
HC7T2: Implement an Using the Color
type from HC7T1, implement the Ord
type class so that Red < Green < Blue.
HC7T3: Implement a function using multiple constraints
Write a function compareValues
that takes two arguments of type a
and returns the larger one. Ensure that a
is both an instance of Eq
and Ord
.
Show
and Read
HC7T4: Create a custom type and implement Define a data type Shape
with constructors Circle Double
and Rectangle Double Double
. Implement Show
and Read
instances for it.
Num
constraints
HC7T5: Implement a function that uses Write a function squareArea
that calculates the area of a square given its side length. Ensure that the function works with any numeric type.
Integral
and Floating
type classes
HC7T6: Use the Define a function circleCircumference
that takes a radius and returns the circumference. Ensure it works with both Integral
and Floating
numbers.
Bounded
and Enum
HC7T7: Implement a function using Create a function nextColor
that takes a Color
and returns the next color in sequence. If it reaches the last color, it should wrap around.
Read
to parse a value from a string
HC7T8: Use Write a function parseShape
that takes a String
and returns a Shape
. The function should return Nothing
for invalid inputs.
HC7T9: Define a type class with multiple instances
Create a type class Describable
with a method describe
. Implement it for Bool
and Shape
.
HC7T10: Use a function with multiple type class constraints
Write a function describeAndCompare
that takes two Describable
values and returns the description of the larger one.