From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id MAA29464; Thu, 13 Dec 2001 12:11:48 +0100 (MET) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id MAA29352 for caml-list@pauillac.inria.fr; Thu, 13 Dec 2001 12:11:48 +0100 (MET) Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id BAA20916 for ; Thu, 13 Dec 2001 01:18:41 +0100 (MET) Received: from osiris.net-nono.com (ANice-102-1-1-58.abo.wanadoo.fr [193.252.35.58]) by nez-perce.inria.fr (8.11.1/8.11.1) with ESMTP id fBD0Idb06291 for ; Thu, 13 Dec 2001 01:18:40 +0100 (MET) Received: (from pagano@localhost) by osiris.net-nono.com (8.11.2/8.11.2) id fBD0K3w29890; Thu, 13 Dec 2001 01:20:03 +0100 Date: Thu, 13 Dec 2001 01:20:03 +0100 From: Bruno Pagano To: Diego Olivier Fernandez Pons Cc: Vincent.Barichard@info.univ-angers.fr, Caml Subject: Re: [Caml-list] Function call with a list of parameters Message-ID: <20011213012003.A29826@osiris.net-nono.com> References: <15382.13531.184819.989958@helios.info-ua><4.3.2.7.2.20011211145019.028d8e50@arda.pair.com> <15383.12155.733583.353752@helios.info-ua> <00a201c18360$f8135480$074cf2c3@Utilisateur> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.2.5i In-Reply-To: <00a201c18360$f8135480$074cf2c3@Utilisateur>; from FernandezPons@iFrance.com on Wed, Dec 12, 2001 at 11:31:53PM +0100 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk 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 = 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 = val sum3 : int -> int -> int -> 'a = 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 = 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