Haskell Program to Iterate over enum


In Haskell, an enumeration (or "enum") is a type that has a finite set of values. The values are called constructors, and are usually defined using the data keyword.

Here's an example of an enumeration type that represents the four seasons −

data Season = Spring | Summer | Fall | Winter

Enumerations are used to represent things like states, modes, or options that have a fixed set of possible values.

Method 1: Iterate over enumeration type

This method is used to directly iterate over the enumeration type in Haskell. Here, we define the list of all the enumeration values. Instance for showing values, is created. And after iterating over the list each output value is displayed.

Algorithm

  • Step 1 − The data type of color is defined.

  • Step 2 − A list of all the enumeration values is defined.

  • Step 3 − Show the instance of the color is created.

  • Step 4 − Program execution will be started from main function. The main() function has whole control of the program. The list is being iterated.

  • Step 5 − The colors are mapped and the final output is printed.

Example

In the following example, we are going to iterate over enumeration type

data Color = Red | Blue | Green
colors :: [Color]
colors = [Red, Blue, Green]
instance Show Color where
   show Red = "Red"
   show Blue = "Blue"
   show Green = "Green"
main :: IO ()
main = mapM_ print colors   

Output

Red
Blue
Green

Method 2: Iterate over enum in Haskell using a list comprehension

In this method, data Color = Red | Blue | Green deriving (Show) defines an enumeration type Color with three constructors: Red, Blue, and Green and iterate over the values of the Color type, printing "Red", "Blue", "Green" in sequence using mapM_ print [Red .. Green].

Here, the enumFromTo function is defined to only work for the range of Red to Green.

Algorithm

  • Step 1 − Enum is defined with three constructors as, Red, Blue, and Green.

  • Step 2 − Instance of Enum is created by using toEnum, fromEnum, succ and pred.

  • Step 3 − enumFromTo is used to iterate over the enum for the defined range.

  • Step 4 − Program execution will be started from main function. The main() function has whole control of the program.

  • Step 5 − The final output is displayed in sequence by using mapM_ print [Red .. Green] statement.

Example

The following example shows how to Iterate over enum in Haskell using a list comprehension

data Color = Red | Blue | Green deriving (Show)
instance Enum Color where
   toEnum 0 = Red
   toEnum 1 = Blue
   toEnum 2 = Green
   toEnum _ = error "Invalid color"
   fromEnum Red = 0
   fromEnum Blue = 1
   fromEnum Green = 2
   succ Red = Blue
   succ Blue = Green
   succ Green = Red
   pred Blue = Red
   pred Green = Blue
   pred Red = Green
   enumFromTo Red Green = [Red, Blue, Green]
   enumFromTo _ _ = error "Invalid range"
main :: IO ()
main = mapM_ print [Red .. Green]

Output

Red
Blue
Green

Method 3: Iterate over enumeration type by using iterate and take.

In this method, the iteration will occur over the Color type by applying the nextSeason function to the initial value of Red 3 times, and then printing each result using mapM_ print. The iterate function creates an infinite list of values by repeatedly applying the nextSeason function to the initial value. The take function is then used to take the first 3 values of this list.

Algorithm

  • Step 1 − Enum is defined with three constructors as, Red, Blue, and Green.

  • Step 2 − nextSeason function is defined over the color constructors.

  • Step 3 − Program execution will be started from main function. The main() function has whole control of the program. The enum is being iterated.

  • Step 4 − Child class instance is being called with Parent1 and Parent2 functions as parameter.

  • Step 5 − The final output is displayed in sequence by using mapM_ print (take 3(iterate nextSeason Red)) statement.

Example

In this example, we are going to learn how to Iterate over enumeration type by using iterate and take.

data Color = Red | Blue | Green deriving (Show)
nextSeason :: Color -> Color
nextSeason Red = Blue
nextSeason Blue = Green
nextSeason Green = Red
main :: IO ()
main = mapM_ print (take 3 (iterate nextSeason Red))

Output

Red
Blue
Green

Conclusion

In Haskell, an enumeration (or "enum") is a type that has a finite set of values. The values are called constructors, and are usually defined using the data keyword.

To iterate over an enumeration type, you can use a combination of functions like mapM_, forM_, enumFromTo, iterate and take. You can also use the toEnum and fromEnum functions to convert between the enumeration values and their corresponding integers.

Updated on: 19-Jan-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements