Skip to main content
edited body
Source Link
Jimmy Hoffa
  • 16.1k
  • 3
  • 70
  • 80
someFunc a b c d = a + b + c + d
plusOne   = someFunc 1
plusTwo   = someFunc 1 1
plusThree = someFunc 1 1 1
    Fourfour  = someFunc 1 1 1 1
someFunc :: a -> (b -> (c -> (d -> (e))))
       plusOne :: b -> (c -> (d -> (e)))
             plusTwo :: c -> (d -> (e))
                 plusThree :: d -> (e)
                            Fourfour :: e
someFunc a b c d = a + b + c + d
plusOne   = someFunc 1
plusTwo   = someFunc 1 1
plusThree = someFunc 1 1 1
    Four  = someFunc 1 1 1 1
someFunc :: a -> (b -> (c -> (d -> (e))))
       plusOne :: b -> (c -> (d -> (e)))
             plusTwo :: c -> (d -> (e))
                 plusThree :: d -> (e)
                            Four :: e
someFunc a b c d = a + b + c + d
plusOne   = someFunc 1
plusTwo   = someFunc 1 1
plusThree = someFunc 1 1 1
    four  = someFunc 1 1 1 1
someFunc :: a -> (b -> (c -> (d -> (e))))
       plusOne :: b -> (c -> (d -> (e)))
             plusTwo :: c -> (d -> (e))
                 plusThree :: d -> (e)
                            four :: e
added 396 characters in body
Source Link
Jimmy Hoffa
  • 16.1k
  • 3
  • 70
  • 80

So in closing, currying works about like so:

given:

someFunc a b c d = a + b + c + d
plusOne   = someFunc 1
plusTwo   = someFunc 1 1
plusThree = someFunc 1 1 1
    Four  = someFunc 1 1 1 1

The signatures would be:

someFunc :: a -> (b -> (c -> (d -> (e))))
       plusOne :: b -> (c -> (d -> (e)))
             plusTwo :: c -> (d -> (e))
                 plusThree :: d -> (e)
                            Four :: e

So in closing, currying works about like so:

given:

someFunc a b c d = a + b + c + d
plusOne   = someFunc 1
plusTwo   = someFunc 1 1
plusThree = someFunc 1 1 1
    Four  = someFunc 1 1 1 1

The signatures would be:

someFunc :: a -> (b -> (c -> (d -> (e))))
       plusOne :: b -> (c -> (d -> (e)))
             plusTwo :: c -> (d -> (e))
                 plusThree :: d -> (e)
                            Four :: e
added 1350 characters in body
Source Link
Jimmy Hoffa
  • 16.1k
  • 3
  • 70
  • 80

tl;dr

This is called currying, also sometimes schoenfinkelization and it's built-in default behaviour in Haskell and derivative languages. Plausibly other languages to I'm unfamiliar with.

http://en.wikipedia.org/wiki/Currying

In mathematics and computer science, currying is the technique of transforming a function that takes n multiple arguments (or an n-tuple of arguments) in such a way that it can be called as a chain of functions, each with a single argument (partial application). It was originated by Moses Schönfinkel[1] and later re-discovered by Haskell Curry.[2][3] Because of this, some say it would be more correct to name it schönfinkeling.[4][5]


Unnecessary details on currying:

Any language with higher order functions can have this behaviour mimicked, generics greatly help it be not clumsy, but the least clumsy languages for this are functional ones with facilities specifically to do it.

That said here are some articles about doing it in various languages to illustrate as you said it is a pattern:

http://msmvps.com/blogs/jon_skeet/archive/2012/01/30/currying-vs-partial-function-application.aspx

http://mtomassoli.wordpress.com/2012/03/18/currying-in-python/

https://wiki.php.net/rfc/currying

By default in haskell, every function takes no more than one value and returns one value, until eventually it takes no values and returns a value.

This is written as:

someFunc :: inputVal -> (returnVal)

This becomes currying as:

someFunc :: inputVal1 -> (inputVal2 -> (inputVal3 -> (returnVal))))

In this example, someFunc takes a value, and returns (a function which takes a value and returns (a function which takes a value and returns (a value)))).

Each of the functions in that chain take one value, the last return is not a function as it does not take anything but rather holds a computation (perhaps 1+2, something that doesn't take parameters).

I say this is default behaviour in Haskell because in Haskell, passing 2 values to someFunc will hand it one value, and hand the next value to the returned function, returning the 3rd function in the chain.

i.e. using the signature above, if I defined someFunc as:

someFunc x y z = x + y + z

then I call it as:

plusTwo :: inputVal3 -> (returnVal)
plusTwo = someFunc 1 1

Now I have captured the 2nd returned function in the chain by giving it one parameter which causes it to return the first function, and the second parameter I gave it is curried into that first returned function causing it to be called and return the 3rd function, which I use as the value for plusTwo

This is called currying, also sometimes schoenfinkelization and it's built-in default behaviour in Haskell and derivative languages. Plausibly other languages to I'm unfamiliar with.

http://en.wikipedia.org/wiki/Currying

In mathematics and computer science, currying is the technique of transforming a function that takes n multiple arguments (or an n-tuple of arguments) in such a way that it can be called as a chain of functions, each with a single argument (partial application). It was originated by Moses Schönfinkel[1] and later re-discovered by Haskell Curry.[2][3] Because of this, some say it would be more correct to name it schönfinkeling.[4][5]

By default in haskell, every function takes no more than one value and returns one value, until eventually it takes no values and returns a value.

This is written as:

someFunc :: inputVal -> (returnVal)

This becomes currying as:

someFunc :: inputVal1 -> (inputVal2 -> (inputVal3 -> (returnVal))))

In this example, someFunc takes a value, and returns (a function which takes a value and returns (a function which takes a value and returns (a value)))).

Each of the functions in that chain take one value, the last return is not a function as it does not take anything but rather holds a computation (perhaps 1+2, something that doesn't take parameters).

I say this is default behaviour in Haskell because in Haskell, passing 2 values to someFunc will hand it one value, and hand the next value to the returned function, returning the 3rd function in the chain.

i.e. using the signature above, if I defined someFunc as:

someFunc x y z = x + y + z

then I call it as:

plusTwo :: inputVal3 -> (returnVal)
plusTwo = someFunc 1 1

Now I have captured the 2nd returned function in the chain by giving it one parameter which causes it to return the first function, and the second parameter I gave it is curried into that first returned function causing it to be called and return the 3rd function, which I use as the value for plusTwo

tl;dr

This is called currying, also sometimes schoenfinkelization and it's built-in default behaviour in Haskell and derivative languages. Plausibly other languages to I'm unfamiliar with.

http://en.wikipedia.org/wiki/Currying

In mathematics and computer science, currying is the technique of transforming a function that takes n multiple arguments (or an n-tuple of arguments) in such a way that it can be called as a chain of functions, each with a single argument (partial application). It was originated by Moses Schönfinkel[1] and later re-discovered by Haskell Curry.[2][3] Because of this, some say it would be more correct to name it schönfinkeling.[4][5]


Unnecessary details on currying:

Any language with higher order functions can have this behaviour mimicked, generics greatly help it be not clumsy, but the least clumsy languages for this are functional ones with facilities specifically to do it.

That said here are some articles about doing it in various languages to illustrate as you said it is a pattern:

http://msmvps.com/blogs/jon_skeet/archive/2012/01/30/currying-vs-partial-function-application.aspx

http://mtomassoli.wordpress.com/2012/03/18/currying-in-python/

https://wiki.php.net/rfc/currying

By default in haskell, every function takes no more than one value and returns one value, until eventually it takes no values and returns a value.

This is written as:

someFunc :: inputVal -> (returnVal)

This becomes currying as:

someFunc :: inputVal1 -> (inputVal2 -> (inputVal3 -> (returnVal))))

In this example, someFunc takes a value, and returns (a function which takes a value and returns (a function which takes a value and returns (a value)))).

Each of the functions in that chain take one value, the last return is not a function as it does not take anything but rather holds a computation (perhaps 1+2, something that doesn't take parameters).

I say this is default behaviour in Haskell because in Haskell, passing 2 values to someFunc will hand it one value, and hand the next value to the returned function, returning the 3rd function in the chain.

i.e. using the signature above, if I defined someFunc as:

someFunc x y z = x + y + z

then I call it as:

plusTwo :: inputVal3 -> (returnVal)
plusTwo = someFunc 1 1

Now I have captured the 2nd returned function in the chain by giving it one parameter which causes it to return the first function, and the second parameter I gave it is curried into that first returned function causing it to be called and return the 3rd function, which I use as the value for plusTwo

added 1350 characters in body
Source Link
Jimmy Hoffa
  • 16.1k
  • 3
  • 70
  • 80
Loading
added 1350 characters in body
Source Link
Jimmy Hoffa
  • 16.1k
  • 3
  • 70
  • 80
Loading
Source Link
Jimmy Hoffa
  • 16.1k
  • 3
  • 70
  • 80
Loading