What is happening when I compose * with + in Haskell?

前端 未结 5 1227
臣服心动
臣服心动 2020-12-24 00:59

I\'m trying to understand the result of

(*) . (+) 

in Haskell. I know that the composition operator is just the standard composition of ma

5条回答
  •  庸人自扰
    2020-12-24 01:44

    Let:

    m = (*)
    a = (+)
    

    then

    (m.a) x = (m (a x)) = m (a x) 
    

    Now m expects a Num a as a parameter, on the other hand (a x) , i.e. (x +) is a unary function (a -> a) by definition of (+). I guess what happened is that GHC tries to unite these two types so that, if you have a type that is both a number and a unary function, m can take a number and a unary function and return a unary function, since they are considered the same type.

    As @Syd pointed, this unification wouldn't make sense for any normal number types such as integers and floating point numbers.

提交回复
热议问题