4

GHCi内から実行するhaskellで電卓を作成しました。ただし、最終的な数値は整数または double になる可能性があるため、型宣言を行いました

calc :: String -> Either Integer Double

ただし、関数の出力には、たとえば、常にその前に左または右があります

Left 7

Right 8.4

左と右が印刷されるのを止める方法はありますか?

4

2 に答える 2

9

ファンシーな方法

(おそらく、以下の他のあまり派手でないソリューションの方が適しています)

ghci だけに関心がある場合は、(GHC>=7.6)カスタム印刷機能を使用する可能性があります。次のように指定するだけです。

type CalcResult = Either Integer Double

calcPrint :: CalcResult -> IO()
calcPrint (Left intg) = print intg
calcPrint (Right floatng) = print floatng

次に、ghciをロードします

$ ghci YourModule.hs -interactive-print=YourModule.calcPrint SpecPrinter

このようにすると、少し煩わしくなります:calcPrintしか使えないCalcResultので、それ以外は何も表示できません。これを回避するには、型クラスを使用できます。

{-# LANGUAGE FlexibleInstances    #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE OverlappingInstances #-}

data CalcResult            -- We must prevent the 'Show' instance of 'Either' from
   = IntegerResult Integer -- getting in our way. (This type is better anyway,
   | FloatingResult Double -- you might want to add more types (e.g. 'Complex')
                           -- later, which is no good with 'Either'.)

class CalcShow c where
  calcShow :: c -> String

instance (Show c) => CalcShow c where
  calcShow = show

instance CalcShow CalcResult where
  calcShow (IntegerResult intg) = show intg
  calcShow (FloatingResult floatng) = show floatng

calcPrint :: CalcShow c => c -> IO()
calcPrint = putStrLn . calcShow

Showこのようにして、古いクラスのものと同様に、計算結果を好きなように表示できます。

$ ghci-7.6 GHCI_Customprint.hs -interactive-print=GHCI_Customprint.calcPrint
GHCi、バージョン 7.6.2: http://www.haskell.org/ghc/ :? ヘルプ
パッケージ ghc-prim を読み込んでいます ... リンクしています ... 完了しました。
パッケージ integer-gmp をロード中 ... リンク中 ... 完了。
パッケージベースを読み込んでいます...リンクしています...完了。
[1/1] GHCI_Customprint のコンパイル (GHCI_Customprint.hs、解釈済み)
わかりました。モジュールがロードされました: GHCI_Customprint。
*GHCI_Customprint> "blubb"
"blubb"
*GHCI_Customprint> [1..5]
[1,2,3,4,5]
*GHCI_Customprint> IntegerResult 39
39
*GHCI_Customprint> FloatingResult $ -236.24983e+89
-2.3624983e91

もう一つの解決策

私が言ったように、結果としてではなく、カスタム データ型を使用する必要がありますEither。そのようなタイプがある場合は、必要なことを行うShowインスタンスを与えることもできます。

instance Show CalcResult where
  show (IntegerResult intg) = show intg
  show (FloatingResult floatng) = show floatng

あなたの目的のためには、これはおそらく問題なく、追加の調整なしで ghci で使用でき、必要なことが行われます。Showただ、インスタンスが有効な Haskell コードを生成するという一種の法則があります。しかし、それは実際には問題ありませ3ん。27.8CalcResult

instance Num CalcResult where
  fromInteger = IntegerResult
  IntegerResult a + IntegerResult b = IntegerResult $ a+b
  ...

instance Floating CalcResult where
  fromRational = FloatingResult . fromRational
  ...
于 2013-04-28T20:19:44.607 に答える
9

この関数を評価すると、GHCi は自動的putStrLn . showに結果を呼び出します。と文字列を追加する関数showです。Either Integer DoubleLeftRight

これを回避するeither show showには、代わりに を使用できます。これにより、 内に格納されている数値にのみ関数が適用されますshowEither

> putStrLn . either show show $ calc ...

あなたが望むものをあなたに与えるべきです。

于 2013-04-28T20:23:21.540 に答える