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 FAA18494; Thu, 20 Nov 2003 05:42:14 +0100 (MET) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id FAA18823 for ; Thu, 20 Nov 2003 05:42:12 +0100 (MET) X-SPAM-Warning: Sending machine is listed in blackholes.five-ten-sg.com Received: from dell.nogin.org (24-205-142-186.pas-mres.charterpipeline.net [24.205.142.186]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id hAK4gB112700 for ; Thu, 20 Nov 2003 05:42:11 +0100 (MET) Received: from cs.caltech.edu (localhost.localdomain [127.0.0.1]) by dell.nogin.org (8.12.10/8.12.9) with ESMTP id hAK4fCge022475; Wed, 19 Nov 2003 20:41:12 -0800 Message-ID: <3FBC45E8.8070603@cs.caltech.edu> Date: Wed, 19 Nov 2003 20:41:12 -0800 From: Aleksey Nogin Organization: California Institute of Technology, Computer Science Department User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.6a) Gecko/20031023 X-Accept-Language: ru, en-us, en MIME-Version: 1.0 To: Peter Scott , Caml List Subject: Re: [Caml-list] Recursive apply function References: <5.2.0.9.0.20031119204912.00a9fb28@127.0.0.1> In-Reply-To: <5.2.0.9.0.20031119204912.00a9fb28@127.0.0.1> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Loop: caml-list@inria.fr X-Spam: no; 0.00; caml-list:01 imitate:01 recursively:01 626:99 ocaml:01 ocaml:01 lisp:01 int:01 rec:01 caltech:01 caltech:01 match:02 wrote:03 recursive:03 types:03 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk On 19.11.2003 19:54, 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;; > > let add x y = x + y;; > let x = apply add [2; 3];; > print_int x;; > > As you can see, the idea is to go recursively applying the function to > the first element of the list. Unfortunately, I get this message when > run: "This expression has type 'a but is here used with type 'b -> 'a", > referring to the "(f arg)" part. > > Is there a way around this? Well, note that your "apply" function would only make sense for fixed-length lists (e.g. "apply add [2; 3; 4]" is not right). In OCaml, fixed-length lists are called "tuples". ;-) -- Aleksey Nogin Home Page: http://nogin.org/ E-Mail: nogin@cs.caltech.edu (office), aleksey@nogin.org (personal) Office: Jorgensen 70, tel: (626) 395-2907 ------------------- 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