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 LAA05116; Tue, 6 Mar 2001 11:34:35 +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 LAA04971 for ; Tue, 6 Mar 2001 11:34:34 +0100 (MET) Received: from smtp2.ihug.co.nz (smtp2.ihug.co.nz [203.109.252.8]) by nez-perce.inria.fr (8.11.1/8.10.0) with ESMTP id f26AYUD16709 for ; Tue, 6 Mar 2001 11:34:31 +0100 (MET) Received: from [192.168.0.12] (p55-max4.wlg.ihug.co.nz [203.173.230.247]) by smtp2.ihug.co.nz (8.9.3/8.9.3/Debian 8.9.3-21) with ESMTP id XAA32260; Tue, 6 Mar 2001 23:34:17 +1300 X-Authentication-Warning: smtp2.ihug.co.nz: Host p55-max4.wlg.ihug.co.nz [203.173.230.247] claimed to be [192.168.0.12] Mime-Version: 1.0 X-Sender: bruce@209.163.245.148 Message-Id: In-Reply-To: <4.3.2.7.2.20010306012957.00c7cf00@shell16.ba.best.com> References: <4.3.2.7.2.20010306012957.00c7cf00@shell16.ba.best.com> Date: Tue, 6 Mar 2001 23:33:27 +1300 To: Chris Hecker , caml-list@inria.fr From: Bruce Hoult Subject: Re: [Caml-list] currying... Content-Type: text/plain; charset="us-ascii" ; format="flowed" Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk At 1:39 AM -0800 6/3/01, Chris Hecker wrote: >How does caml know when to call a function? For example, say I have: > >val f: int -> int -> int -> unit > >and the definition of f is > >let f x y = Printf.printf "%d %d" x y;Printf.printf "%d" > >so f actually takes two ints, prints them, and then returns a >function that takes an int and returns unit. From the val >declaration above in a .cmi file, how can caml tell the difference >between that f and this one: > >let f x y z = Printf.printf "%d %d %d" x y z > >How does it know "when" to call f, since you need a different number >of parameters for the different definitions? The top f prints x y >when it's called with two parms, so it doesn't wait until all three >parms have been passed. > >I have a feeling I'm missing something fundamental here, or else the >definition of a function internally has a field for its arity and it >just partially applies until it reaches the total arity. I thought >I remembered seeing some documentation on this months ago, but I >can't find it now... > >It doesn't seem to partially evaluate the function or anything >insane like that. Let me have an attempt at this, even though I'm a beginner too :-) In OCaml, *all* functions actually take *one* argument. When you write... let f x y = Printf.printf "%d %d" x y;Printf.printf "%d" ... it is actually just shorthand for a function that takes *one* argument (x) and returns as it's result a function that takes *one* argument (y) and prints x and y and then returns a function that takes one argument and prints it. val f: int -> int -> int -> unit .. actually means... val f: int -> (int -> (int -> unit))) .. it's just that since -> is right-associative you don't need the brackets. Your first function would be perhaps better written as: val f: int -> int -> (int -> unit) But you don't need the parens :-) In Scheme your first function would be like this: (define f (lambda (x) (lambda (y) (display x) (display " ") (display y) (lambda (a) (display a))))) (((f 1) 2) 3) How does an OCaml function know how many arguments to take? It's *always* one. So you don't need the parens that you do in scheme. And if a function returns another function then applying it to the next argument in line is automatic. This would of course be very inefficient if the compiler didn't do clever things... -- Bruce ------------------- To unsubscribe, mail caml-list-request@inria.fr. Archives: http://caml.inria.fr