# Question regarding pattern matching functions based on input type

**URL:** <https://forum.elixirforum.com/t/question-regarding-pattern-matching-functions-based-on-input-type/3217>\
**Category:** Questions\
**Tags:** pattern-matching, book-programming-elixir\
**Created:** [January 10, 2017, 4:01am UTC](https://forum.elixirforum.com/t/question-regarding-pattern-matching-functions-based-on-input-type/3217 "2017-01-10T04:01:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![wktdev](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/wktdev/32/17649_2.png) [@wktdev](https://forum.elixirforum.com/u/wktdev)\
**Post date:** [January 10, 2017, 4:01am UTC](https://forum.elixirforum.com/t/question-regarding-pattern-matching-functions-based-on-input-type/3217/1 "2017-01-10T04:01:21Z")

</div>

I understand pattern matching in a very basic sense.

So for example I understand perfectly well what this code does

> %{william: age} = %{william: 40 }

> IO.inspect age # 40

> {a, b} = {100, 200}

> IO.inspect a # 100

> IO.inspect b # 200

What I am having trouble understanding is function behavior when named functions with the same name and arity take precedence over one another based on argument type

So for example, the following code is an implementation of a len function and it returns the length of a list. For the most part I understand how it works ( I think ). The two functions allow for behavior usually reserved for conditional statements. If the array is empty then the recursion is terminated

> defmodule Mylist do  
> def len(), do: 0  
> def len([head|tail]), do: 1 + len(tail)  
> end

> IO.inspect Mylist.len([5, 4, 3, 2, 1]) # 5

The previous functions only work if the argument type is a list. If I did the following I get an error:

> IO.inspect Mylist.len(“blah”) # no function clause matching in Mylist.len/1

Based on this, I assume there is a way to write functions that only respond to inputs that are of type number or string. Is this possible without using conditional statements?

---

<div class="post-metadata">

**Author:** ![andre1sk](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/andre1sk/32/1380_2.png) [@andre1sk](https://forum.elixirforum.com/u/andre1sk)\
**Post date:** [January 10, 2017, 4:30am UTC](https://forum.elixirforum.com/t/question-regarding-pattern-matching-functions-based-on-input-type/3217/2 "2017-01-10T04:30:09Z")

</div>

It possible to do using guard clause e.g.  
def blah(a) when is\_integer(a) , do: a  
[https://hexdocs.pm/elixir/master/guards.html](https://hexdocs.pm/elixir/master/guards.html)

---

<div class="post-metadata">

**Author:** ![wktdev](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/wktdev/32/17649_2.png) [@wktdev](https://forum.elixirforum.com/u/wktdev)\
**Post date:** [January 10, 2017, 4:31am UTC](https://forum.elixirforum.com/t/question-regarding-pattern-matching-functions-based-on-input-type/3217/3 "2017-01-10T04:31:16Z")

</div>

Cool, thanks. I read about guard clauses but didn’t think to use them. I’m trying to learn to “think” like an Elixir dev

---

<div class="post-metadata">

**Author:** ![OvermindDL1](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/overminddl1/32/2677_2.png) [@OvermindDL1](https://forum.elixirforum.com/u/OvermindDL1)\
**Post date:** [January 10, 2017, 3:10pm UTC](https://forum.elixirforum.com/t/question-regarding-pattern-matching-functions-based-on-input-type/3217/4 "2017-01-10T15:10:14Z")

</div>

You can also match on the string type (binary) itself. 🙂

```elixir
def takes_only_string(<<the_string::binary>>), do: the_string

```

For integers, like andre1sk’s example, a guard clause is required (and there are guard clauses for `is_binary/1` too). 🙂

---

<div class="post-metadata">

**Author:** ![bobbypriambodo](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/bobbypriambodo/32/2843_2.png) [@bobbypriambodo](https://forum.elixirforum.com/u/bobbypriambodo)\
**Post date:** [January 10, 2017, 3:38pm UTC](https://forum.elixirforum.com/t/question-regarding-pattern-matching-functions-based-on-input-type/3217/5 "2017-01-10T15:38:32Z")

</div>

And if you want to go further, let’s pattern match on the content of the string itself:

```elixir
def strip_prefix("prefix" <> rest), do: rest

strip_prefix("foo") # FunctionClauseError
strip_prefix("prefixfoo") # => foo

```

But note that you can’t do this:

```elixir
def strip_suffix(rest <> "suffix"), do: rest
# CompileError, a binary field without size is only allowed at the end of a binary pattern

```

You must specify the size:

```elixir
def strip_suffix(<<rest::binary-size(4)>> <> "suffix"), do: rest

strip_suffix("foo") # FunctionClauseError
strip_suffix("foosuffix") # FunctionClauseError
strip_suffix("fooosuffix") # => fooo

```
