[erlang-questions] Help with tuple pattern match?

Richard A. O'Keefe <>
Thu Jun 19 02:47:12 CEST 2008


On 19 Jun 2008, at 7:04 am, Caitlin Gibbons wrote:
> Thanks for the prompt reply. Could you explain the details of your  
> answer? I interpreted the structure of this form of pattern matching  
> as:
>
> Person={person,{name,{first,joe},{last,armstrong}},{footsize,42}}.
>
> {person,_,{name,_{first,_{last,_}},{footsize,_}=Person.
>

You have	{person,{name,{first,joe},{last,armstrong}},{footsize,42}}
you want	                                ^^^^^^^^^
you write       {person,{name,{first,_},  {last,Last     }}, 
{footsize,_ }}
or else  	{_     ,{_   ,_          ,{_   ,Last     }},_            }

replacing all the bits you are not interested in with wild-cards.

The first approach keeps the 'attribute name' atoms and replaces all
the uninteresting 'attribute value' constants with wild-cards.
That's good to use if you are not perfectly sure that your source will
be providing you with data of the correct form.

The second approach replaces _everything_ except the part you want and
the lists or tuples above it with wild-cards.

Me, I'd go for the first approach because it is much much easier to get
the right number of curly braces and commas that way.







More information about the erlang-questions mailing list