Part 4 - Higher-order functionsExercisesType-inference by yourselfDocumentation and user's manualTable of contentsOCaml programs

Type-inference by yourself

To check your solution enter an Objective Caml toplevel from a directory containing the files  List_1.cmi  and  List_1.cmo  (in other words: the module  List_1 ) to be extracted from  this archive . Then in this toplevel first evaluate this Caml code:

#load "List_1.cmo" ;;

open List_1 ;;

This loads the module  List_1  then opens it, which allows to use short names instead of long ones in dotted notation (that is, for instance  cons_list  instead of  List_1.cons_list ).

 1 
  Consider each of the following types. Write a value having this types (naming it is useless), evaluate it, then apply it:
 (int -> int) -> int -> int  that is:  (int -> int) -> (int -> int) 
 (int -> bool) -> int -> bool  that is:  (int -> bool) -> (int -> bool) 

 2 
  All the following Caml phrases are syntactically correct. They are assumed to be evaluated in order. Consider each of them. Is it a value definition let ... = e or an expression e? In each case e has a value. Which one and of which type?

let mul = function f -> (function (x, y) -> exp (f (log (x), log (y)))) ;;
let integer = function f -> (function x -> int_of_float (f (float_of_int (x)))) ;;
let comp = function 
  (f1, f2, f3) -> (function x -> let (a, b) = f1 (x) in f3 (f2 (a), f2 (b))) ;;
let times = mul (function (a, b) -> a +. b) ;;
times (3., 5.) ;;
let root = integer (sqrt) ;;
root (81) ;;
let c = comp ((function x -> x / 4, x mod 4), 
              (function x -> x * 3), 
              (function q, r -> 4 * q + r)) ;;
c (9) ;;
let map_to_each = function
  f -> (let rec g = function
          lx ->  if is_empty_list (lx)
                   then empty_list ()
                   else cons_list (f (head (lx)), g (tail (lx)))
        in
          g) ;;
map_to_each (function x -> 2 * x) ;;
(map_to_each (function x -> 2 * x)) ([1 ; 2 ; 3]) ;;
map_to_each (function x -> 3 * x + 1) ;;
(map_to_each (function x -> 3 * x + 1)) ([1 ; 2 ; 3]) ;;
List.map ;;
List.map (function x -> 2 * x) ;;
(List.map (function x -> 2 * x)) ([1 ; 2 ; 3]) ;;
List.map (function x -> 3 * x + 1) ;;
(List.map (function x -> 3 * x + 1)) ([1 ; 2 ; 3]) ;;
let curry = function f -> (function x -> (function y -> f (x, y))) ;;
let uncurry = function f -> (function (x, y) -> (f (x)) (y)) ;;
let plus = function x, y -> x + y ;;
let plus_curry = function x -> (function y -> x + y) ;;
plus (3, 7) ;;
curry (plus) ;;
(curry (plus)) (3) ;;
((curry (plus)) (3)) (7) ;;
plus_curry (3) ;;
(plus_curry (3)) (7) ;;
uncurry (plus_curry) ;;
(uncurry (plus_curry)) (3, 7) ;;
let exchange = function f -> (function x -> (function y -> (f (y)) (x))) ;;
let rec accum = function
  (op, neutral) -> (let rec
                      f = function
                        lx ->  if is_empty_list (lx)
                                 then neutral
                                 else op (head (lx), f (tail (lx)))
                    in
                      f) ;;
let accum_1 = function
  f -> exchange (function neutral -> accum (uncurry (f), neutral)) ;;
accum_1 (function x -> (function y -> x + y)) ;;
(accum_1 (function x -> (function y -> x + y))) ([1 ; 2 ; 3]) ;;
((accum_1 (function x -> (function y -> x + y))) ([1 ; 2 ; 3])) (7) ;;
((accum_1 (function x -> (function y -> x - y))) ([1 ; 2 ; 3])) (7) ;;
List.fold_right ;;
((List.fold_right (function x -> (function y -> x + y))) ([1 ; 2 ; 3])) (7) ;;
((List.fold_right (function x -> (function y -> x - y))) ([1 ; 2 ; 3])) (7) ;;
let rec fp = function f -> (function x -> if x = f (x) then x else (fp (f)) (f (x))) ;;
fp (function x -> (x +. 2.) /. (x +. 1.)) ;;
(fp (function x -> (x +. 2.) /. (x +. 1.))) (0.) ;;


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

Part 4 - Higher-order functionsExercisesType-inference by yourselfDocumentation and user's manualTable of contentsOCaml programs