- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
- Related Articles
- Java Program to Iterate over enum
- Java Program to Iterate over an ArrayList
- Java Program to Iterate over a HashMap
- Java Program to Iterate over a Set
- Swift Program to Iterate Over an Array
- Golang program to iterate over a Slice
- C++ Program to Iterate Over an Array
- C++ Program to Iterate Over a Dictionary
- Swift Program to Iterate Over an Set
- Python program to iterate over multiple lists simultaneously?
- Java Program to Iterate over ArrayList using Lambda Expression
- Program to iterate over a List using Java 8 Lambda
- Golang Program To Iterate Over Each Element From The Arrays
- C++ Program to iterate over each element from the arrays
- C# program to iterate over a string array with for loop
