[Haskell-cafe] Comparison between fields of each record

20 views
Skip to first unread message

Jeon-Young Kang

unread,
Nov 24, 2015, 1:20:12 PM11/24/15
to The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell, Haskel...@haskell.org
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

Francesco Ariis

unread,
Nov 24, 2015, 2:03:35 PM11/24/15
to Jeon-Young Kang, The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell, Haskel...@haskell.org
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
test.hs

Jeon-Young Kang

unread,
Nov 24, 2015, 2:14:08 PM11/24/15
to Jeon-Young Kang, The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell, Haskel...@haskell.org
Thank you so much :) 
--
Department of Geography
State University of New York at Buffalo

 
Jeon-Young Kang
Reply all
Reply to author
Forward
0 new messages