Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Haskell takes one argument

I'm trying to figure out the cause and effect logics behind "Currying" and comes to the following results:

  1. Multiple arguments function can be expressed as either tuple(combining multiple arguments with different types as one argument) and list(combining multiple arguments with the same type as one argument). So all functions can be expressed as single argument function.
  2. So in Haskell, function only takes one argument。How can we implementing multiple-arguments function.
  3. Using Currying. Currying is a way to implementing multiple arguemnts function.

I think the above logic 1 -> 2 -> 3 is confusing to answer "why using currying". For statement 3, one can also combine multiple tuples or lists as single argument to implementing multiple arguments function.

It seems statement 1 is not correct reason for statement 2. What I'm sure is 2->3 is correct reasoning, but what's the reason behind 2? Why Haskell, as a functional language, only take one argument?

Most books take statement 2 as an accepted fact. Does anybody know the reason behind 2? Or does anybody know the correct logic behind "why currying"?

like image 311
Johnson Avatar asked May 05 '16 01:05

Johnson


2 Answers

The decision to use currying as the preferred method of multi-argument functions was made for two reasons. Firstly, Haskell has its roots firmly in lambda calculus, which uses currying. Secondly, currying allows for easy partial application, a language facet which is indescribably useful.

like image 60
Kwarrtz Avatar answered Sep 18 '22 19:09

Kwarrtz


The function

f x y = x + y

has the type (assuming x & y are Integers)

f :: Integer -> Integer -> Integer

this is the same as

g :: Integer -> (Integer -> Integer)

Which means f is a function that takes an Integer and returns a new function that also takes an Integer and returns an Integer. Currying is the process whereby a function that takes multiple arguments is actually evaluated as a series of functions that take one argument.

Currying makes it very easy to partially apply functions as follows

plusTwo :: Integer -> Integer
plusTwo = f 2

The above code simply applies 2 to f and binds the name plusTwo to the resulting function.

This makes it extremely easy for developers to abstract common behavior into a single place.

like image 29
Chris Avatar answered Sep 21 '22 19:09

Chris