Making numeric functions an instance of Num?

后端 未结 2 499
孤独总比滥情好
孤独总比滥情好 2020-12-10 04:26

I want to be able to compose numeric functions in haskell using binary operators. So, for example, with unary numeric functions:

f*g

should

2条回答
  •  余生分开走
    2020-12-10 05:06

    an instance with generic arity

    instance Num b => Num (a->b) where
        f + g = \x -> f x + g x
        f - g = \x -> f x - g x
        f * g = \x -> f x * g x
        negate f = negate . f
        abs f = abs . f
        signum f = signum . f
        fromInteger n = \x -> fromInteger n
    

    Edit: As Christian Conkle points out, there are problems with this approach. If you plan to use these instances for anything important or just want to understand the issues, you should read the resources he provided and decide for yourself whether this fits your needs. My intention was to provide an easy way to play with numeric functions using natural notation with as simple an implementation as possible.

提交回复
热议问题