以下の 2 つの戦略のうち、関数をオーバーロードするのに最も効率的なのはどれでしょうか (ここでは、私の例では関数 teX を使用します)。
使用
data
とパターン マッチング:data TeX = TeXt String | TeXmath String deriving (Show,Read,Eq) teX (TeXt t) = t teX (TeXmath t) = "$$" ++ t ++ "$$"
または、少しの抽象化を使用します。
class TeX t where teX :: t -> String newtype TeXt = TeXt String deriving (Show,Read,Eq) instance TeX TeXt where teX (TeXt t) = t newtype TeXmath = TeXmath String deriving (Show,Read,Eq) instance TeX TeXmath where teX (TeXmath t) = "$$" ++ t ++ "$$"
確かに、前者の方が使いやすく、後者の方が充実しやすいです。しかし、一方が他方よりも速く実行されるのか、それとも Haskell がそれらをまったく同じ方法で実装するのかは疑問です。