# Functional pattern matching

The functional pattern matching it's an extension of the multiple dispatch feature, and allows us to call a certain function or method using values or expressions as patterns, instead of types.

Example:

```ruby
func foo((1)) { say "one" }
func foo((2)) { say "two" }
func foo (n)  { say n }

foo(1)      # calls the first function
foo(2)      # calls the second function
foo(3)      # calls the third function
```

Taking advantage of this feature, we can write the Fibonacci function in the following way:

```ruby
func fib((0)) { 0 }
func fib((1)) { 1 }
func fib (n)  { fib(n-1) + fib(n-2) }

say fib(12)    # prints: 144
```

In addition, instead of an expression, we can specify a block of code, which will get called with the value of the argument for checking:

```ruby
func fib({.is_neg})  { NaN }
func fib({.is_zero}) { 0 }
func fib({.is_one})  { 1 }
func fib(n)          { fib(n-1) + fib(n-2) }

say fib(12)    # prints: 144
```

For keeping the value of the argument, we can specify a parameter name in front of the block used for pattern matching:

```ruby
func fib(n { _ <= 1 }) { n }
func fib(n)            { fib(n-1) + fib(n-2) }

say fib(12)    # prints: 144
```

Pattern matching is available for methods as well:

```ruby
class Ackermann {
    method A({ .is_zero }, n) {
        n + 1
    }

    method A(m, (0)) {
        self.A(m-1, 1)
    }

    method A(m, n) {
        self.A(m-1, self.A(m, n-1))
    }
}

var obj = Ackermann()
say obj.A(3, 2)             # prints: 29
```


---

# 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://trizen.gitbook.io/sidef-lang/syntax_and_semantics/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.
