r/purescript • • Sep 22 '19

[Help] Convert if-then-else to case

I was trying to implement the *Ordering* typeclass instance for a custom data type that I wrote. I am running into problem when using the case expression as below:

data Quadrilateral  
 = Square Int  
  | Rectangle Int Int  

instance eq :: Eq Quadrilateral where  
  eq (Square x) (Square y) = x == y  
  eq (Rectangle x y) (Rectangle v w) = x == v && y == w  
  eq _ _ = false  

instance compare :: Ordering Quadrilateral where  
  compare (Square x) (Square y) = case x, y of  
    x == y -> EQ  
    x < y -> LT  
    x > y -> GT

I presume the problem is that can't have evaluations case expression, right? If so what's an idiomatic way to write this? Don't want to go the if-then-else way.

[EDIT]: Code blocks + Indentation

2 Upvotes

3 comments sorted by

View all comments

Show parent comment

1

u/jetblackgreen Sep 22 '19

Huh. Thanks!

2

u/gb__ Sep 22 '19

You could probably just derive the instances also, I'm not totally sure what you'd want the ordering behaviour to be so it might not be exactly right, but it certainly be fine for Eq at least.

derive instance eqQuadrilateral :: Eq Quadrilateral
derive instance ordQuadrilateral :: Ord Quadrilateral