[erlang-questions] Help with tuple pattern match?

David Mercer <>
Wed Jun 18 23:20:53 CEST 2008


Let's take a look at the tuple:

{person,{name,{first,joe},{last,armstrong}},{footsize,42}}.

The last name is the second element of a 2-tuple, whose first element is the
atom "last", so we would match that with:

{last,Last}

or since we don't care about the first element:

{_,Last}

That tuple, however, is the third element of a 3-tuple, so that is matched
thus:

{_,_,{_,Last}}

Finally, that tuple is the second element of a 3-tuple, resulting in the
final match expression:

{_,{_,_,{_,Last}},_}

DBM

 

 

 

  _____  

From: 
[mailto:] On Behalf Of Caitlin Gibbons
Sent: Wednesday, June 18, 2008 14:05
To: 
Subject: Re: [erlang-questions] Help with tuple pattern match?

 

Hi David.

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.

Is this correct? From left to right, one counts the atoms, i.e.,
person,name,first,last,etc. and 'aligns' them with the underscores?

Thanks again,

Caitlin

 

 

 

On 6/18/08, David Mercer <> wrote:

1> Person={person,{name,{first,joe},{last,armstrong}},{footsize,42}}.

{person,{name,{first,joe},{last,armstrong}},{footsize,42}}

2> {_,{_,_,{_,Last}},_} = Person.

{person,{name,{first,joe},{last,armstrong}},{footsize,42}}

3> Last.

armstrong

 

  _____  

From: 
[mailto:] On Behalf Of Caitlin Gibbons
Sent: Wednesday, June 18, 2008 13:37
To: 
Subject: [erlang-questions] Help with tuple pattern match?

 

Hi all.

I'm looking at:

Person={person,{name,{first,joe},{last,armstrong}},{footsize,42}}.

and trying to extract the value of 'last', i.e., 'armstrong'.

{_,{_,{_,{_,Last},_}=Person.

was unsuccessful. Any help?

Thanks,

Caitlin

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20080618/a73efc65/attachment.html>


More information about the erlang-questions mailing list