惯用函数重载

Ran*_*ize -3 haskell

我google了很多但仍然困惑于在Haskell中重载函数的最惯用的方法.鉴于:

foo :: Int -> String -> Double
foo :: String -> String -> Double
foo :: String -> Double
foo :: Char -> Integer-> Integer -> Integer -> String
Run Code Online (Sandbox Code Playgroud)

(不确定我是否涵盖了所有可能的情况)你会如何使它成为惯用的(类型类?)

fre*_*yle 5

从技术上讲,你可以这样做:

{-# LANGUAGE FlexibleInstances #-}                                          

class Foo a where
    foo :: a

instance Foo (Int -> String -> Double) where
    foo = ...

instance Foo (String -> String -> Double) where
    foo = ...

instance Foo (String -> Double) where
    foo = ...

instance Foo (Char -> Integer -> Integer -> String) where
    foo = ...
Run Code Online (Sandbox Code Playgroud)

但是,我认为这通常是不好的方式,因为我相信这些函数具有随机属性,你只需将它们组合起来就可以用你的思想进行分组,将来有人会很难理解这个foo函数.