Comparison between fields of each record

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

Comparison between fields of each record

Jeon-Young Kang
Dear All.

I'd like to compare fields of each record.

here is my record.

data Person = Person {name:: String, age:: Int } deriving(Show)
data Relations = Friend | Older | Younger

class Comparison a where
    compare:: a -> a -> Relations

instance Comparison Person where
    compare Person a b Person a b
         | b1 == b2 = Friend
         | b1 > b2 = Older
         | b1 < b2 = Younger

How can I fit it?

Sincerely,

 
Jeon-Young Kang

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

Re: Comparison between fields of each record

Dimitri DeFigueiredo
I don't understand your question but the class Ord in Data.Ord is our tool for comparisons. You may want to look into that. You can also
use the function "on" from Data.Function to compare on a specific record within a larger structure.

Dimitri


On 11/24/15 11:20 AM, Jeon-Young Kang wrote:
Dear All.

I'd like to compare fields of each record.

here is my record.

data Person = Person {name:: String, age:: Int } deriving(Show)
data Relations = Friend | Older | Younger

class Comparison a where
    compare:: a -> a -> Relations

instance Comparison Person where
    compare Person a b Person a b
         | b1 == b2 = Friend
         | b1 > b2 = Older
         | b1 < b2 = Younger

How can I fit it?

Sincerely,

 
Jeon-Young Kang


_______________________________________________
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: [Haskell-cafe] Comparison between fields of each record

Francesco Ariis
In reply to this post by Jeon-Young Kang
On Tue, Nov 24, 2015 at 01:20:03PM -0500, Jeon-Young Kang wrote:

> Dear All.
>
> I'd like to compare fields of each record.
>
> here is my record.
>
> data Person = Person {name:: String, age:: Int } deriving(Show)
> data Relations = Friend | Older | Younger
>
> class Comparison a where
>     compare:: a -> a -> Relations
>
> instance Comparison Person where
>     compare Person a b Person a b
>          | b1 == b2 = Friend
>          | b1 > b2 = Older
>          | b1 < b2 = Younger
>
> How can I fit it?
>
> Sincerely,
>
>
> Jeon-Young Kang
Hello Jeon-Young, I attach a version that compiles. Keep in mind that

    compare (Person x y) (Person q w) -- this is legal

    compare Person x y Person q w -- "space" takes precedence over everything,
                                  -- so this function has 6 arguments
                                  -- instead of the expected 2!


λ> Main.compare (Person "cdsac" 1) (Person "cdscasd" 20)
Younger

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

test.hs (355 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: [Haskell-cafe] Comparison between fields of each record

Jeon-Young Kang
Thank you so much :) 

On Tue, Nov 24, 2015 at 2:01 PM, Francesco Ariis <[hidden email]> wrote:
On Tue, Nov 24, 2015 at 01:20:03PM -0500, Jeon-Young Kang wrote:
> Dear All.
>
> I'd like to compare fields of each record.
>
> here is my record.
>
> data Person = Person {name:: String, age:: Int } deriving(Show)
> data Relations = Friend | Older | Younger
>
> class Comparison a where
>     compare:: a -> a -> Relations
>
> instance Comparison Person where
>     compare Person a b Person a b
>          | b1 == b2 = Friend
>          | b1 > b2 = Older
>          | b1 < b2 = Younger
>
> How can I fit it?
>
> Sincerely,
>
>
> Jeon-Young Kang

Hello Jeon-Young, I attach a version that compiles. Keep in mind that

    compare (Person x y) (Person q w) -- this is legal

    compare Person x y Person q w -- "space" takes precedence over everything,
                                  -- so this function has 6 arguments
                                  -- instead of the expected 2!


λ> Main.compare (Person "cdsac" 1) (Person "cdscasd" 20)
Younger



--
Department of Geography
State University of New York at Buffalo

 
Jeon-Young Kang

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

Re: [Haskell-cafe] Comparison between fields of each record

MJ Williams
Okay, can I ask you to provide a little more explanation of what you
want to perform with your code? To me, at least, the question, "How
can I fit it?" is a little too terse.

I think before you even set up a new data, class, class instances
etc., the most key step is to put in words, the simpler and cleaer the
better, exactly what you what the code to do for you. Pseudo code in
prose, if you like. You'll find that with clarity of thought expressed
in words comes a greater command of the programming language, or at
least, a clearer idea of what you want from the programming language.

Matthew


On 24/11/2015, Jeon-Young Kang <[hidden email]> wrote:

> Thank you so much :)
>
> On Tue, Nov 24, 2015 at 2:01 PM, Francesco Ariis <[hidden email]> wrote:
>
>> On Tue, Nov 24, 2015 at 01:20:03PM -0500, Jeon-Young Kang wrote:
>> > Dear All.
>> >
>> > I'd like to compare fields of each record.
>> >
>> > here is my record.
>> >
>> > data Person = Person {name:: String, age:: Int } deriving(Show)
>> > data Relations = Friend | Older | Younger
>> >
>> > class Comparison a where
>> >     compare:: a -> a -> Relations
>> >
>> > instance Comparison Person where
>> >     compare Person a b Person a b
>> >          | b1 == b2 = Friend
>> >          | b1 > b2 = Older
>> >          | b1 < b2 = Younger
>> >
>> > How can I fit it?
>> >
>> > Sincerely,
>> >
>> >
>> > Jeon-Young Kang
>>
>> Hello Jeon-Young, I attach a version that compiles. Keep in mind that
>>
>>     compare (Person x y) (Person q w) -- this is legal
>>
>>     compare Person x y Person q w -- "space" takes precedence over
>> everything,
>>                                   -- so this function has 6 arguments
>>                                   -- instead of the expected 2!
>>
>>
>> λ> Main.compare (Person "cdsac" 1) (Person "cdscasd" 20)
>> Younger
>>
>
>
>
> --
> Department of Geography
> State University of New York at Buffalo
>
> [hidden email]
>
> Jeon-Young Kang
>
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners