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

前端 未结 5 1251
臣服心动
臣服心动 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:50

    There are good answers here, but let me quickly point out a few steps where you went wrong.

    First, the correct definition of function composition is

    (f . g) x = f (g x)
    

    you omitted the x on the LHS. Next, you should remember that in Haskell h x y is the same as (h x) y. So, contrary to what you expected,

    ((*) . (+)) 1 2 = (((*) . (+)) 1) 2 = ((*) ((+) 1)) 2 = ((+) 1) * 2,
    

    and now you see why that fails. Also,

    ((*) . (+)) 1 (\x -> x + 1) 1
    

    does not work, because the constraint Num (Int -> Int) is not satisfied.

提交回复
热议问题