[erlang-questions] guards

Dave Pawson <>
Sat Jul 25 12:00:12 CEST 2009


2009/7/25 Johnny Billquist <>:

>>> A pattern of [X] can only match a list with one element.
>>
>> Couldn't X be a variable which might be a tuple or ... almost anything?
>> Or is this pattern matching logic, that implies it is as you say?
>
> I'm making the assumption that X is unbound at the time, otherwise we have a
> different ballgame where much less will match (in fact, only a list, of
> which the only element is identical to whatever X is bound to).

Yes. Thanks. I hadn't gone that far. No matter what ?type? it is,
there is only one of them in the list!



>
> The *only* element in the list could, however, be a list, a tuple, or just
> about anything. There is no limits to what it could be. The point is, that
> that is the only element in the list.

Two stage logic. It's a list length one, then deal with the list item.
I'll get there eventually!


> No need to use guards to check for that. However, if the list should hold
> one element, and that should be a tuple, but you don't know what kind of
> tuple, then you'd need a guard, as in:
>
> foo([X]) when is_tuple(X) ->
>
> because the pattern can't specify a tuple with a variable number of
> elements.

Thanks.



-- 
Dave Pawson
XSLT XSL-FO FAQ.
Docbook FAQ.
http://www.dpawson.co.uk


More information about the erlang-questions mailing list