Pattern Matching for record syntax

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
10 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









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

Re: 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/ )

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

signature.asc (815 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Pattern Matching for record syntax

Imants Cekusins
In reply to this post by Jeon-Young Kang

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


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

Re: Pattern Matching for record syntax

Jeon-Young Kang
In reply to this post by Magnus Therning
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 <[hidden email]> wrote:

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/ )

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners



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

Re: Pattern Matching for record syntax

Imants Cekusins
In reply to this post by Imants Cekusins

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


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

Re: Pattern Matching for record syntax

Imants Cekusins
In reply to this post by Jeon-Young Kang

> 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
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Pattern Matching for record syntax

Jeon-Young Kang
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 <[hidden email]> wrote:

> 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
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners



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

Re: Pattern Matching for record syntax

Imants Cekusins

Could you filter, then map? I'd try that instead of pattern matching.


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

Re: Pattern Matching for record syntax

Magnus Therning
In reply to this post by Jeon-Young Kang

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: [hidden email]   jabber: [hidden email]
twitter: magthe               http://therning.org/magnus

Finagle's Fifth Law:
Always draw your curves, then plot your readings.

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

signature.asc (815 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Pattern Matching for record syntax

Jeon-Young Kang
I  figured out.. Thank you so much.

On Thu, Jan 7, 2016 at 5:51 PM, Magnus Therning <[hidden email]> wrote:

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: [hidden email]   jabber: [hidden email]
twitter: magthe               http://therning.org/magnus

Finagle's Fifth Law:
Always draw your curves, then plot your readings.

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners



_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners