]>
On Wed, Dec 12, 2001 at 11:31:53PM +0100, Diego Olivier Fernandez Pons wrote:
One wants write the following function
# let rec app f = function
[] -> f
| x::l -> app (f x) l ;;
but caml reply :
This expression has type 'a but is here used with type 'b -> 'a
Thanks to OOP in caml ;-) , we can use cyclics types (-rectypes option)
and now :
# let rec app f = function
[] -> f
| x::l -> app (f x) l ;;
val app : ('b -> 'a as 'a) -> 'b list -> 'a = <fun>
So what about the first argument of app. I need a function with 'a -> 'b
as type, it must be a function with no termination. Ok, the result may
be raised by an exception.
For instance :
exception Int of int
# let sum2 x y = raise (Int (x+y))
let sum3 x y z = raise (Int (x+y+z)) ;;
val sum2 : int -> int -> 'a = <fun>
val sum3 : int -> int -> int -> 'a = <fun>
To end, the apply function will catch the result :
# let apply f l = try app f l ; 0 with Int x -> x ;;
Warning: this function application is partial,
maybe some arguments are missing.
val apply : ('b -> 'a as 'a) -> 'b list -> int = <fun>
We are more intelligent than the type checker and we ommit the warning ...
and now let's try :
# apply sum2 [1;2] ;;
- : int = 3
# apply sum3 [1;2;3] ;;
- : int = 6
Bruno Pagano
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr