[Haskell-beginners] Exposing Ratio data constructor

Casey Rodarmor caseyrodarmor at gmail.com
Mon Sep 29 12:17:44 EDT 2008

Hej hej!

I was writing some code using Ratio, and even though I know it's
tucked behind an abstraction barrier, I really wanted access to the
Ratio data constructor ':%'. I wrote invertRatio like such:

invertRatio r = denominator r % numerator

But I really wanted to write it like this:

invertRatio (n :% d) = d % n

I understand that exposing ':%' causes problems, since it allows us
not only to pick apart ratios, but to construct bad ones that would
normally be caught when constructed with '%'. (Such as '1:%0'.)

Is there any way to avoid this, while still letting the user benefit
from the nice pattern matching syntax that exposing the data
constructor allows?

Casey Rodarmor

More information about the Beginners mailing list