In Haskell, + is a function, (+ 2) is a function, (+ 2 3) is 5. What exactly is going on there? -


  1. how possible, going on there?

  2. is there name this?

  3. what other languages have same behavior?

  4. any without strong typing system?

actually (+ 2 3) type error. (+) 2 3 or (+ 2) 3 give 5.

this behaviour simple , intuitive if @ types. avoid complications of infix operators +, i'm going use function plus instead. i'm going specialise plus work on int, reduce typeclass line noise.

say have function plus, of type int -> int -> int. 1 way read "a function of 2 ints returns int". notation little clumsy reading, isn't it? return type isn't singled out specially anywhere. why write function type signatures way? because -> right associative, equivalent type int -> (int -> int). looks more it's saying "a function int (a function int int)". 2 types in fact same, , latter interpretation key understanding how behaviour works.

haskell views functions being single argument single result. there may computations have in mind result of computation depends on 2 or more inputs (such plus). haskell says function plus function takes single input, , produces output another function. second function takes single input , produces output number. because second function computed first (and different different inputs first function), "final" output can depend on both inputs, can implement computations multiple inputs these functions take single inputs.

i promised easy understand if looked @ types. here's example expressions types explicitly annotated:

plus       :: int -> int -> int plus 2     ::        int -> int plus 2 3   ::               int 

if function , apply argument, type of result of application need remove first arrow function's type. if leaves type has more arrows, still have function! add arguments right of expression, remove parameter types left of type. type makes clear type of intermediate results are, , why plus 2 function can further applied (its type has arrow) , plus 2 3 not (its type doesn't have arrow).

"currying" process of turning function of 2 arguments function of 1 argument returns function of argument returns whatever original function returned. it's used refer property of languages haskell automatically have functions work way; people haskell "is curried language" or "has currying", or "has curried functions".

note works particularly elegantly because haskell's syntax function application simple token adjacency. free read plus 2 3 application of plus 2 arguments, or application of plus 2 , application of result 3; can mentally model whichever way fits you're doing @ time.

in languages c-like function application parenthesised argument list, breaks down bit. plus(2, 3) different plus(2)(3), , in languages syntax 2 versions of plus involved have different types. languages kind of syntax tend not have functions curried time, or have automatic currying of function like. such languages have historically tended not have functions first class values, makes lack of currying moot point.


Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c# - Copy ObservableCollection to another ObservableCollection -

All overlapping substrings matching a java regex -