Giving too many arguments to a function in Haskell -


in haskell, can give few arguments function curried function:

-- addition function prelude> :t (+)  (+) :: num => -> ->  -- curried first argument => increment function prelude> :t (+) 1 (+) 1 :: num => ->  -- supplied both arguments => result prelude> :t (+) 1 2 (+) 1 2 :: num => 

but when supply many arguments?

prelude> :t (+) 1 2 3 (+) 1 2 3 :: (num a, num (a -> t)) => t 

what this, have name, , useful anything?

it's useful think way. always give 1 argument function. f b equivalent (f a) b.

so (+) 1 2 3 same as

 (((+) 1) 2) 3 

now know ((+) 1) 2 is, it's same (+) 1 2 or 1 + 2 3. expression boils down 3 3.

how come it's not error?

integer literals in haskell overloaded. 3 of any type, provided type has num instance. there nothing illegal give num instance function type either. type inferencer tells that:

(num a, num (a -> t)) => t 

this can read as

for type a has num instance, , type a->t has num instance, expression in question has type t.

of course in practice such instances unlikely, 1 can in principle define them, , make expression (+) 1 2 3 evaluate well-defined value.


Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -