# [erlang-questions] guards

Johnny Billquist <>
Sat Jul 25 11:03:41 CEST 2009

```Dave Pawson wrote:
> 2009/7/25 Hynek Vychodil <>:
>>
>> On Sat, Jul 25, 2009 at 4:50 AM, Dave Pawson <> wrote:
>>> filterHelper(Lst,Int,Result) when ((length(Lst) == 1) and
>>> (lists:nth(1,Lst) =< Int)) ->
>>>    [lists:nth(1,Lst) | Result];
>> It is little bit overcomplicated version of
>>
>> filterHelper([X], Int, Result) when X =< Int -> [X|Result];
>
> Mmm. Thanks... I think :-)
> (I'm trying to learn!)
>
> Does that check for a list length of 1?

A pattern of [X] can only match a list with one element.
If the list have two elements, you'd have to write [X,Y] to match, and
so on...
If you want to match a list of unknown length, where you want to look at
the first element, you should write [X|Y]. Y will then be the list with
all the remaining elements after the first, which is bound to X.
Check the first two elements by [X,Y|Z], and so on...

And of course, to match a list with no elements, you write []. :-)

Johnny

--
Johnny Billquist                  || "I'm on a bus
||  on a psychedelic trip