I have the following requirements: given a rational number (x % y):
- If y != 1 => return "x y"
- Otherwise: return "x"
The following function works:
import Data.Ratio
my_show :: (Integral a, Show a) => (Ratio a) -> String
my_show rat = let x = numerator rat
y = denominator rat in
if y /= 1 then (show x) ++ " " ++ (show y) else show x
Is it possible to make it more elegant? For example, I saw in the sources of Data.Ratio that it uses some notation for functions inputs: (x:%y)
, but it didn't work for me. So I have to use let
and explicitly call numerator
and denominator
.