Pattern Matching for record syntax

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

Pattern Matching for record syntax

Jeon-Young Kang
Dear All.

Hope your 2016 is off to a great start.

I would like to get results from pattern matching or something.

Here is the my code.

data Person = Person {name :: String, age :: Int}

names = ["tom", "sara"] -- list of names, String

persons = [Person {name = "tom", age = 10}, Person {name="sara", age=9},
                  Person {name = "susan", age = 8}].

Is there any solution to get the age of "tom" and "sara"?

I have no idea of pattern matching for this one.

I've tried to use recursion, but I couldn't find any solution for list of records.

Sincerely,
Young









_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: [Haskell-beginners] Pattern Matching for record syntax

Magnus Therning

Jeon-Young Kang writes:

> Dear All.
>
> Hope your 2016 is off to a great start.
>
> I would like to get results from pattern matching or something.
>
> Here is the my code.
>
> data Person = Person {name :: String, age :: Int}
>
> names = ["tom", "sara"] -- list of names, String
>
> persons = [Person {name = "tom", age = 10}, Person {name="sara", age=9},
>                   Person {name = "susan", age = 8}].
>
> Is there any solution to get the age of "tom" and "sara"?
>
> I have no idea of pattern matching for this one.
>
> I've tried to use recursion, but I couldn't find any solution for list of
> records.
How about using `filter`[1] over `persons` with a function checking if
the name is in `names`?

I'm sorry, but this sounds like home work so you won't get more than
this from me.

/M

[1]: http://hackage.haskell.org/package/base-4.8.1.0/docs/Data-List.html#v:filter

--
Magnus Therning              OpenPGP: 0x927912051716CE39
email: [hidden email]   jabber: [hidden email]
twitter: magthe               http://therning.org/magnus

Would you go to war without a helmet? Would you drive without the seat
belt?  Then why do you develop software as if shit doesn’t happen?
     -- Alberto G ( http://makinggoodsoftware.com/2009/05/12/hdd/ )

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe

signature.asc (815 bytes) Download Attachment