Pattern Matching for record syntax

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

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: magnus@therning.org jabber: magnus@therning.org 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/ )

Thanks Magnus Therning.
Actually, this is NOT homework. I am just studying Haskell for my current
research.
I've tried "filter" before. But I couldn't reach what I want to do.
it seems that filter is only applicable to list, not "record syntax".
Do I need functor for this??
Best,
On Thu, Jan 7, 2016 at 5:24 PM, 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: magnus@therning.org jabber: magnus@therning.org 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/ )
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

For pattern matching.. I implemented like this.
compareName a (Person name age)
| (a == name) = age
but, I got stuck how to apply pattern matching for the two lists.
i.e., both names and persons are lists.
I really appreciate your advice..
On Thu, Jan 7, 2016 at 5:37 PM, Imants Cekusins
filter is only applicable to list, not "record syntax".
but [Person] is the input, isn't it?
Do I need functor for this??
no need to define new functor instance to filter over a list of records.
but you could use fmap over list, yes
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

Jeon-Young Kang writes:
For pattern matching.. I implemented like this.
compareName a (Person name age) | (a == name) = age
but, I got stuck how to apply pattern matching for the two lists. i.e., both names and persons are lists.
I really appreciate your advice..
Something like this works: map age $ filter (\ p -> (name p) `elem` names) persons /M -- Magnus Therning OpenPGP: 0x927912051716CE39 email: magnus@therning.org jabber: magnus@therning.org twitter: magthe http://therning.org/magnus Finagle's Fifth Law: Always draw your curves, then plot your readings.

I figured out.. Thank you so much.
On Thu, Jan 7, 2016 at 5:51 PM, Magnus Therning
Jeon-Young Kang writes:
For pattern matching.. I implemented like this.
compareName a (Person name age) | (a == name) = age
but, I got stuck how to apply pattern matching for the two lists. i.e., both names and persons are lists.
I really appreciate your advice..
Something like this works:
map age $ filter (\ p -> (name p) `elem` names) persons
/M
-- Magnus Therning OpenPGP: 0x927912051716CE39 email: magnus@therning.org jabber: magnus@therning.org twitter: magthe http://therning.org/magnus
Finagle's Fifth Law: Always draw your curves, then plot your readings.
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

you could filter record list http://hackage.haskell.org/package/base-4.8.1.0/docs/Prelude.html#v:filter \r -> elem (name r) names then fmap the filtered list to extract age age <$> list1

if you must use pattern matching, here is a tip: (Person name0 age0) i.e. records are like tuples with values in the order of declared record fields. name0 can be named anything else. To match any field value, use _ for that field
participants (3)
-
Imants Cekusins
-
Jeon-Young Kang
-
Magnus Therning