 |
|
| |
|
 |
 |
at Global Oneness Community.
Share your dreams and let others help you with the interpretation!
Dream Sharing Forum
|
 |
Currying - Examples |  | Currying - Examples: Encyclopedia II - Currying - Examples |  | Suppose that plus is a function taking two arguments x and y and returning x + y. In the ML programming language we would define it as follows:
plus = fn(x, y) => x + y
and plus(1, 2) returns 3 as we expect.
The curried version of plus takes a single argument x and returns a new function which takes a single argument y and returns x + y. In ML we would define it as follows:
curried_plus = fn(x) => fn(y) => x + y
and now when we call curried_plus(1) we get a new ...
See also:Currying, Currying - Examples, Currying - Mathematical view |  | | Currying, Currying - Examples, Currying - Mathematical view, Lazy evaluation, Closure (computer science), S-m-n theorem |  | |
|  |  | Currying: Encyclopedia II - Currying - Examples
Currying - Examples
Suppose that plus is a function taking two arguments x and y and returning x + y. In the ML programming language we would define it as follows:
plus = fn(x, y) => x + y
and plus(1, 2) returns 3 as we expect.
The curried version of plus takes a single argument x and returns a new function which takes a single argument y and returns x + y. In ML we would define it as follows:
curried_plus = fn(x) => fn(y) => x + y
and now when we call curried_plus(1) we get a new function that adds 1 to its argument:
plus_one = curried_plus(1)
and now plus_one(2) returns 3 and plus_one(7) returns 8.
When declaring functions in the strictly-typed OCaml programming language, the type returned by a function shows the Curried form of the function. Typing the function into the OCaml interpreter displays the type immediately:
# let plus x y = x + y ;;
val plus : int -> int -> int = <fun>
Here is a way to do currying in C++ (from this comment on codepost.org):
The plus function:
int plus(int x, int y) {
return x + y;
}
The curried version of plus:
class curried_plus {
private:
int x;
public:
curried_plus(int _x) : x(_x) { ; }
int operator () (int y) const {
return plus(x, y);
}
};
and the usage:
curried_plus plus_one(1);
now plus_one(2) returns 3.
Other related archivesC++, Cartesian closed category, Christopher Strachey, Closure (computer science), Functional programming, Gottlob Frege, Haskell, Haskell Curry, JavaScript, Lambda calculus, Lazy evaluation, Lisp, ML, ML programming language, Mathematical logic, Moses Schönfinkel, OCaml programming language, Perl, Python, Ruby, S-m-n theorem, adjoint functors, arguments, computer science, first-class objects, function, interpreter, isomorphic, lambda calculus, programming languages, syntactic sugar, theorem, theoretical computer science
 Adapted from the Wikipedia article "Examples", under the G.N U Free Docmentation License. Please also see http://en.wikipedia.org/wiki |
|
|
More material related to Currying can be found here:
|
|
« Back
|
Search the Global Oneness web site |
|
|
|
|
 |
Sneak-Peek of Global Oneness Community
Hi friend! The Global Oneness Community, the place for information and sharing about Oneness is not really launched yet (you will see there is still some clean up to do) ...but it is now open for a sneak-peek! And if you wish - please register and become one of the very first members to do so! Jonas
Forum Home,
Articles,
Photo Gallery,
Videos,
News,
Sitemap
...and much more!
|