Higher-order: two well-known examplesTutorial (draft)Functions - Abstraction and application againDocumentation and user's manualTable of contentsOCaml programs

Functions - Abstraction and application again

   
  Recall.

First three values:

2 + (3 * 4) ;;

5 + (0 * 8) ;;

4 + (1 * 7) ;;

Then the same values expressed using local definitions:

let
  a = 2 and b = 3 and c = 4
in
  a + (b * c) ;;

let
  a = 5 and b = 0 and c = 8
in
  a + (b * c) ;;

let
  a = 4 and b = 1 and c = 7
in
  a + (b * c) ;;

Now the abstraction step:

function a, b, c -> a + (b * c) ;;

Finally the application step (three times):

(function a, b, c -> a + (b * c)) (2, 3, 4) ;;

(function a, b, c -> a + (b * c)) (5, 0, 8) ;;

(function a, b, c -> a + (b * c)) (4, 1, 7) ;;

   
  Recall again. Here the startpoint consists of values of expressions containing applications of functions. As the abstraction step deals with these functions it leads to a higher-order functional value whose parameters are functional.

First three values:

3.  *. sin (2.)       +. 0.5 *. cos (4.) ;;

7.2 *. abs_float (3.) +. 8.  *. exp (6.1) ;;

5.  *. log (10.5)     +. 3.1 *. sin (0.7) ;;

Then the same values expressed using local definitions:

let
  a = 3. and b = sin and c = 2. and d = 0.5 and e = cos and f = 4.
in
  a *. b (c) +. d *. e (f) ;;

let
  a = 7.2 and b = abs_float and c = 3. and d = 8. and e = exp and f = 6.1
in
  a *. b (c) +. d *. e (f) ;;

let
  a = 5. and b = log and c = 10.5 and d = 3.1 and e = sin and f = 0.7
in
  a *. b (c) +. d *. e (f) ;;

Now the abstraction step:

function a, b, c, d, e, f -> a  *. b (c) +. d *. e (f) ;;

Finally the application step (three times):

(function a, b, c, d, e, f -> a  *. b (c) +. d *. e (f)) (3., sin, 2., 0.5, cos, 4.) ;;

(function a, b, c, d, e, f -> a  *. b (c) +. d *. e (f)) (7.2, abs_float, 3., 8., exp, 6.1) ;;

(function a, b, c, d, e, f -> a  *. b (c) +. d *. e (f)) (5., log, 10.5, 3.1, sin, 0.7) ;;

   
  More about higher-order functions.

Here the startpoint consists of functional values. As the abstraction step deals with these functions it leads to a higher-order functional value whose results of applications are functional values.

First three values:

function x -> 1 + x ;;

function x -> 2 + x ;;

function x -> 3 + x ;;

Then the same values expressed using local definitions:

let
  z = 1
in
  function x -> z + x ;;

let
  z = 2
in
  function x -> z + x ;;

let
  z = 3
in
  function x -> z + x ;;

Now the abstraction step:

function z -> (function x -> z + x) ;;

Then the application step (three times):

(function z -> (function x -> z + x)) (1) ;;

(function z -> (function x -> z + x)) (2) ;;

(function z -> (function x -> z + x)) (3) ;;

Finally compare applications of results of applications of this functional value with applications of the initial functional values:

(function x -> 1 + x) (5) ;;

((function z -> (function x -> z + x)) (1)) (5) ;;

(function x -> 2 + x) (7) ;;

((function z -> (function x -> z + x)) (2)) (7) ;;

(function x -> 3 + x) (9) ;;

((function z -> (function x -> z + x)) (3)) (9) ;;

   
  Three examples of a function whose applications result in functions (note that the value bound to  unary_of_int_add  is that in the previous example):

(** Returns the function that adds x and an integer, given the integer x *)
(* *)
let unary_of_int_add = function x -> (function z -> x + z) ;;

(*****)

(** Returns the function that multiplies x and a real number, given the real number x *)
(* *)
let unary_of_real_mult = function x -> (function z -> x *. z) ;; 

(*****)

(** Returns the function that concatenates x before a string, given the string x *)
(* *)
let unary_of_string_concat = function x -> (function z -> x ^ z) ;;

   
  In the sequel will also be written higher-order functions whose parameters are functions and whose results of applications are functions, too.


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

Higher-order: two well-known examplesTutorial (draft)Functions - Abstraction and application againDocumentation and user's manualTable of contentsOCaml programs