[Haskell-beginners] Exposing Ratio data constructor

Chaddaï Fouché chaddai.fouche at gmail.com
Mon Sep 29 15:03:50 EDT 2008


2008/9/29 Casey Rodarmor <caseyrodarmor at gmail.com>:
>
> invertRatio r = denominator r % numerator

Ratio is an instance of Fractional, which means :
invertRation = recip

ou

invert f = 1 / f

(probably the default definition of recip anyway).

> 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?

To this more general question : allow the convenience of pattern
matching while keeping a datatype abstract, the latest GHC (6.10)
bring a new extension called "view pattern", it may not be ideal but
it should be pretty useful in this direction.
http://hackage.haskell.org/trac/ghc/wiki/ViewPatterns

-- 
Jedaï


More information about the Beginners mailing list