# Pattern Matching

**Pattern matching** is similar to conditional statements and allows us to choose different paths for our functions based on the patterns of their arguments. These **patterns can be direct values or more general patterns of arguments** that the function takes in. Much like in [guarded equations](/defining-functions-working-with-functions/conditionals/guarded-equations.md), the patterns are evaluated from top to bottom and the first one that matches the input arguments is selected for further evaluation. We can define our previous function `cardColour` using pattern matching with **direct values**:

```haskell
cardColour :: String -> String
cardColour "hearts" = "red"
cardColour "diamonds" = "red"
cardColour "spades" = "black"
cardColour "clubs" = "black"
cardColour _ = "I am not familiar with this card suit."

ghci> cardColour "diamonds"
"red"
ghci> cardColour "ace"
"I am not familiar with this card suit."
```

That is, we define multiple patterns for our function to account for different card suits and the wildcard character performs the same function as in the [`case`-`of`](/defining-functions-working-with-functions/conditionals/case-of-statements.md) example. Pattern matching gets more powerful when we use it with lists and tuples to build larger patterns.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://haskell.hpmeducation.com/defining-functions-working-with-functions/pattern-matching.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
