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
ahasnuminstance, , typea->thasnuminstance, expression in question has typet.
of course in practice such instances unlikely, 1 can in principle define them, , make expression (+) 1 2 3 evaluate well-defined value.
Comments
Post a Comment