Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

composing functions with higher arity

I don't understand composing functions with arity > 1. in ghci 7.4.1 i typed:

((*).succ) 3 4
> 16

i don't fully understand the math transformation but it's clear that it's the same as

(*) (succ 3) 4

but when i do:

( (\x y z -> x).(\a b -> a*b) ) 2 3 4 5
> 10
( (\x y z -> y).(\a b -> a*b) ) 2 3 4 5
> No instance for (Num (a0 -> t0))

and now i'm totally lost. can anyone explain what happens? ps. I know that everything in haskell has only 1 parameter but it doesn't really help me :)

like image 377
piotrek Avatar asked Sep 19 '13 22:09

piotrek


1 Answers

Work it out this way:

(f . g) x = f (g x)
(f . g) x y = f (g x) y -- applying y

Then replace f with (*), g with succ and x and y with their values:

((*) . succ) 3 4 = (*) (succ 3) 4
                 = (*) 4 4
                 = 16
like image 163
JB. Avatar answered Sep 27 '22 22:09

JB.