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 HAA22827; Thu, 20 Nov 2003 07:32:12 +0100 (MET) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f 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 HAA22135 for ; Thu, 20 Nov 2003 07:32:11 +0100 (MET) Received: from herd.plethora.net (herd.plethora.net [205.166.146.1]) by nez-perce.inria.fr (8.11.1/8.11.1) with ESMTP id hAK6WA122491 for ; Thu, 20 Nov 2003 07:32:10 +0100 (MET) Received: from bhurt.plethora.net (bhurt.plethora.net [205.166.146.49]) by herd.plethora.net (8.11.6/8.10.1) with ESMTP id hAK6VxC22831; Thu, 20 Nov 2003 00:32:07 -0600 (CST) Date: Thu, 20 Nov 2003 01:31:33 -0600 (CST) From: Brian Hurt X-X-Sender: bhurt@localhost.localdomain To: Peter Scott cc: caml-list@inria.fr Subject: Re: [Caml-list] Recursive apply function In-Reply-To: <5.2.0.9.0.20031119204912.00a9fb28@127.0.0.1> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Loop: caml-list@inria.fr X-Spam: no; 0.00; caml-list:01 imitate:01 implemented:01 val:01 val:01 ocaml:01 lisp:01 int:01 rec:01 rec:01 nov:01 match:02 wrote:03 recursive:03 library:03 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk On Wed, 19 Nov 2003, Peter Scott wrote: > I'm having an interesting disagreement with ocaml about types. I'm trying > to make a function to imitate the lisp apply function. That is, I want to > make a function which will apply another function to a list. > > I came up with this code: > > let rec apply f args = > match args with > arg :: rest -> apply (f arg) rest > | [] -> f;; This is untypeable. What's the type of f? It's a function that takes a 'a and returns a function which takes a type 'a and returns a function which takes a type 'a and returns a function which takes a type 'a and etc. What you want is List.fold_left, which has type: ('a -> 'b -> 'a) -> 'a -> 'b list -> 'a and can be implemented as: let rec fold_left f init_val = function | [] -> init_val | h :: t -> fold_left f (f init_val h) t ;; But don't reimplement it, use the library version. > > let add x y = x + y;; > let x = apply add [2; 3];; > print_int x;; let x = List.fold_left (+) 0 [2; 3];; You don't need to define a special add function. -- "Usenet is like a herd of performing elephants with diarrhea -- massive, difficult to redirect, awe-inspiring, entertaining, and a source of mind-boggling amounts of excrement when you least expect it." - Gene Spafford Brian ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners