Tutorial (draft)An accumulatorCurryingDocumentation and user's manualTable of contentsOCaml programs

Currying

   
  Currying is the technique of transforming a function that takes multiple arguments into a function that takes a single argument (the first of the arguments to the original function) and returns a new function that takes the remainder of the arguments and returns the result.

   
  Uncurrying is the reverse transformation.

   
  This a definition of the function (named  curry ) that computes the curried version of a function with two arguments:

let curry = function f -> (function x -> (function y -> f (x, y)))

   
  Here are applications of  curry  that return functional values previously written:

(** The same function as unary_of_int_add. *)
(* *)
curry (function x, y -> x + y) ;;

(*****)

(** The same function as unary_of_real_mult. *)
(* *)
curry (function x, y -> x *. y) ;;

(*****)

(** The same function as unary_of_string_concat. *)
(* *)
curry (function x, y -> x ^ y) ;;

   
  This a definition of the function (named  uncurry ) that uncurries the curried version of a function with two arguments:

let uncurry = function f -> (function (x, y) -> (f (x)) (y))

   
 

Almost all the functions provided by the modules in the libraries of the Objective Caml system only take a single argument.

Multiple-arguments functions can be considered as syntactic sugar for nested one-argument functions.

   
  Here are applications of  curry  that computes addition of two integers, multiplication of two real numbers and concatenation of two strings:

uncurry (unary_of_int_add) ;;

uncurry (unary_of_real_mult) ;;

uncurry (unary_of_string_concat) ;;


Latest update : October 5, 2006
This document was translated from LaTeX by Hyperlatex 2.5, which is not the latest version of Hyperlatex.

Tutorial (draft)An accumulatorCurryingDocumentation and user's manualTable of contentsOCaml programs