In Haskell, the switch statement from many other programming languages is replaced with the case statement, which is a type of pattern matching. However, we can simulate the use of else if in Haskell using multiple case statements within a single function using the case keyword.
Here is an example of how to use case statement as else if in Haskell:
-- Function that determines a person's age group
ageGroup :: Int -> String
ageGroup age = case age of
x | x < 0 -> "Invalid age"
| x < 18 -> "Child"
| x < 40 -> "Young adult"
| x < 60 -> "Middle-aged adult"
| otherwise -> "Senior citizen"
In this code, the function ageGroup takes a single argument: the person's age as an integer. The case statement is used to check the value of age against a series of conditions, using | (or guard) notation. If age satisfies the first condition (age < 0), then "Invalid age" will be returned; if it satisfies the second condition (age < 18), then "Child" will be returned; and so on, until reaching the default clause otherwise which matches any value, returning "Senior citizen" as default age group.
So, calling ageGroup with 25 as argument will return "Young adult". This function can be used to simulate the same functionality as else if block, providing multiple conditions to be checked and handled accordingly in a much cleaner syntax than nested if statements.