combine pattern matching against named fields and tuples

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

combine pattern matching against named fields and tuples

Michael Mossey
I'm trying to do something like this:

data Thing = Thing { field1, field2 :: ( Int, Int ) }

myfunc = Thing { field1 ( _, x ) } = x

but it doesn't work. That is, I want to match against the second item of the tuple
which is the named field1. I'm not just trying to do this particular thing, but
trying to figure out if some kind of general pattern matching can be done like this.

Reply | Threaded
Open this post in threaded view
|

combine pattern matching against named fields and tuples

Brandon S Allbery KF8NH
On 2009 Apr 7, at 3:30, Michael Mossey wrote:
> I'm trying to do something like this:
>
> data Thing = Thing { field1, field2 :: ( Int, Int ) }
>
> myfunc = Thing { field1 ( _, x ) } = x

 > myfunc (Thing {field1 = (_,x)}) = x
using record pattern matching

--
brandon s. allbery [solaris,freebsd,perl,pugs,haskell] [hidden email]
system administrator [openafs,heimdal,too many hats] [hidden email]
electrical and computer engineering, carnegie mellon university    KF8NH


-------------- next part --------------
A non-text attachment was scrubbed...
Name: PGP.sig
Type: application/pgp-signature
Size: 195 bytes
Desc: This is a digitally signed message part
Url : http://www.haskell.org/pipermail/beginners/attachments/20090407/3bf147a7/PGP.bin
Reply | Threaded
Open this post in threaded view
|

combine pattern matching against named fields and tuples

Thomas Davie
In reply to this post by Michael Mossey

On 7 Apr 2009, at 09:30, Michael Mossey wrote:

> I'm trying to do something like this:
>
> data Thing = Thing { field1, field2 :: ( Int, Int ) }
>
> myfunc = Thing { field1 ( _, x ) } = x
>
> but it doesn't work. That is, I want to match against the second  
> item of the tuple which is the named field1. I'm not just trying to  
> do this particular thing, but trying to figure out if some kind of  
> general pattern matching can be done like this.

Firstly, you have an extra equals in there, secondly, in a pattern  
match constructions must go in parentheses, and finally, you're  
missing an equals inside the record:

myFunc (Think {field1 = (_,x)}) = x

Bob