• Javascript
  • Python
  • Go

Haskell Syntax: Case Expression in a Do Block

Haskell is a powerful and elegant functional programming language that has gained popularity among developers in recent years. One of the ke...

Haskell is a powerful and elegant functional programming language that has gained popularity among developers in recent years. One of the key features of Haskell is its ability to handle complex logic and data structures with ease. In this article, we will explore one of the language's fundamental constructs, the case expression, and how it can be used in a do block.

A case expression is a type of conditional statement that allows for pattern matching on a given value. It is similar to a switch statement in other languages, but with more powerful and flexible capabilities. Let's take a closer look at how it works.

The syntax for a case expression in Haskell is as follows:

```

case expression of

pattern1 -> result1

pattern2 -> result2

...

patternN -> resultN

```

The `expression` can be any value or variable, and the `pattern` can be any data type or structure that the `expression` can be matched against. Each pattern is followed by an arrow `->` and then the corresponding result or action to be taken if the pattern matches the expression.

Now, let's see how a case expression can be used in a do block. First, let us define a simple function that takes in an integer and returns a string based on its value.

```

numToString :: Int -> String

numToString num = case num of

0 -> "Zero"

1 -> "One"

2 -> "Two"

_ -> "Other"

```

In the above example, we have used a case expression to match the value of `num` against different patterns and return a string based on the matched pattern. The underscore `_` is used as a wildcard pattern to handle any value that does not match the defined patterns.

Now, let's use this function in a do block to showcase the power of case expressions. We will create a simple program that takes in an integer from the user and prints out the corresponding string.

```

main = do

putStrLn "Enter a number:"

num <- readLn :: IO Int

let result = numToString num

putStrLn $ "The string representation of " ++ show num ++ " is " ++ result

```

In the above code, we use the `readLn` function to read in an integer from the user and bind it to the variable `num`. We then use the `let` keyword to define a variable `result` that stores the output of `numToString` function with `num` as the input. Finally, we use `putStrLn` to print out the result in a user-friendly format.

Let's run this program and see the output for different inputs.

```

Enter a number:

0

The string representation of 0 is Zero

Enter a number:

3

The string representation of 3 is Other

```

As you can see, the case expression in the `numToString` function handles different cases and returns the appropriate string, which is then printed out in the do block.

In addition to simple pattern matching, case expressions in Haskell also support guards, which are boolean expressions that can be used to further refine the pattern matching. Let's see an example of this using our previous function.

```

numToString :: Int -> String

numToString num = case num of

0 -> "Zero"

1 -> "One"

2 -> "Two"

x | x < 0 -> "Negative"

_ -> "Other"

```

In the above code, we have added a guard `x | x < 0` to the pattern matching. This means that if the value of `x` is less than 0, the corresponding result of "Negative" will be returned. This allows for more complex logic to be implemented in a case expression.

In conclusion, the case expression in a do block is a powerful tool in Haskell that allows for pattern matching and handling different cases in a concise and elegant manner. It is a fundamental concept that every Haskell developer should be familiar with. So the next time you are working on a project in Haskell, don't forget to leverage the power of case expressions to make your code more efficient and readable.

Related Articles

Converting IO Int to Int

In the world of programming, data conversion is a crucial concept. It involves the process of converting data from one type to another, ensu...

What Exactly is a Monad?

A monad is a concept that is often used in functional programming languages, particularly in Haskell. It is a powerful abstraction that can ...

Beginner's Guide to Haskell

Haskell is a powerful functional programming language that has gained popularity among developers in recent years. Its elegant syntax and st...