109: Ratio の show が負のケースに未対応

↑up

現象

testcases/torational.hs の実行結果が次のようになる:

7070651414971679 % 2251799813685248
13170115 % 4194304
-5764607523034235 % 1152921504606846976
179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586298239947245938479716304835356329624224137216 % 1
-179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586298239947245938479716304835356329624224137216 % 1
-269653970229347386159395778618353710042696546841345985910145121736599013708251444699062715983611304031680170819807090036488184653221624933739271145959211186566651840137298227914453329401869141179179624428127508653257226023513694322210869665811240855745025766026879447359920868907719574457253034494436336205824 % 1

runhaskell による結果は次のように、分子が負の場合には括弧がつく:

7070651414971679 % 2251799813685248
13170115 % 4194304
(-5764607523034235) % 1152921504606846976
179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586298239947245938479716304835356329624224137216 % 1
(-179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586298239947245938479716304835356329624224137216) % 1
(-269653970229347386159395778618353710042696546841345985910145121736599013708251444699062715983611304031680170819807090036488184653221624933739271145959211186566651840137298227914453329401869141179179624428127508653257226023513694322210869665811240855745025766026879447359920868907719574457253034494436336205824) % 1

2020-10-12 (Mon)

ひとまず、次のようにした:

instance (Integral a) => Show (Ratio a) where
  show (x :% y) | x < 0     = "(" ++ show x ++ ") % " ++ show y
                | otherwise = show x ++ " % " ++ show y

torational.hs → sample297.hs