Question: I am trying to understand how can you pattern match in Erlang for single elements vs lists:

```
guarded(T) when T>5 ; T<3 -> 3+T;
guarded([X,Y]) when X>3,Y>3 ->{X+1,Y+1};
guarded([X,_|[T,_]]) when X rem 2==0, T rem 2 =/= 1-> [T,X];
guarded(_)->"something else".

guarded([1,2,3]).  -> goes into case 1 , how can i make sure it doesn't (and goes to last case)

```

> 
> \*\* exception error: an error occurred when evaluating an arithmetic expression
>  in function main:guarded/1
> 
> 
> 

Where would i need to place the first case , when i want to pattern match on single elements.I mean i want to have a case that works for single elements and a wild-card-pattern too (last expression).

Answer: 
You can add an [`is_list/1`](http://erlang.org/doc/man/erlang.html#is_list-1) check to your guards to check that `T` is not a list:

```
guarded(T) when not is_list(T) andalso (T>5 orelse T<3) -> 3+T;
guarded([X,Y]) when X>3, Y>3 ->{X+1,Y+1};
guarded([X,_|[T,_]]) when X rem 2==0, T rem 2 =/= 1-> [T,X];
guarded(_)->"something else".

```

Or you could use [`is_number/1`](http://erlang.org/doc/man/erlang.html#is_number-1) or [`is_integer/1`](http://erlang.org/doc/man/erlang.html#is_integer-1) to check that `T` is a number or an integer respectively:

```
guarded(T) when is_number(T) andalso (T>5 orelse T<3) -> 3+T;
guarded([X,Y]) when X>3, Y>3 ->{X+1,Y+1};
guarded([X,_|[T,_]]) when X rem 2==0, T rem 2 =/= 1-> [T,X];
guarded(_)->"something else".

```
As an alternative to Vinoski's answer, you can also move the less specific pattern to the end, so lists are matched first:

```
guarded([X,Y]) when X>3,Y>3 ->{X+1,Y+1};
guarded([X,_|[T,_]]) when X rem 2==0, T rem 2 =/= 1-> [T,X];
guarded(T) when T>5 ; T<3 -> 3+T;
guarded(_)->"something else".

```

But in this specific case it won't work well, because something like `guarded([1,1])` will still not match the first two branches but will match the `T` one; this is because Erlang [allows comparing any two values](http://erlang.org/doc/reference_manual/expressions.html#term-comparisons) and simply considers lists greater than numbers.