caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* RE: [Caml-list] currying...
@ 2001-03-06 10:16 Adam Granicz
  0 siblings, 0 replies; 16+ messages in thread
From: Adam Granicz @ 2001-03-06 10:16 UTC (permalink / raw)
  To: caml-list

it does evaluate it partially. you have to remember that functions are
first-class values as well. as a matter of fact, the formally declared
parameters do not really matter, they are resolved by the compiler by
looking at the body of the function. declaring formal parameters is only
necessary when the order of these parameters can not be unambigously
determined. thus

let neg = function
	true -> false
   |  false -> true

is automatically understood as val: neg: bool -> bool.

also, remember that ocaml functions take only one parameter, so

val f: int -> int -> int -> unit

is actually the composition of several functions, the first taking an int
and returning a unit, the second taking an int and returning an int, and so
on.

> so f actually takes two ints, prints them, and then returns a
> function that takes an int and returns unit.  From the val

this one would be

val f: int -> int -> (int -> unit)

anyway, as to your question: if enough arguments are supplied so that the
function can be evaluated, then it is called and returns whatever that
function was supposed to return. however, if not all arguments were given
then the function is only partially evaluated with all known parameters.

let f i j = i+j

f 1 returns a function that expects one argument, so

let g = f 1

and

g 2 return 3.

hope this helps..

adam.

-------------------
To unsubscribe, mail caml-list-request@inria.fr.  Archives: http://caml.inria.fr


^ permalink raw reply	[flat|nested] 16+ messages in thread
* [Caml-list] currying...
@ 2001-03-06  9:39 Chris Hecker
  2001-03-06 10:22 ` Thomas Colcombet
                   ` (3 more replies)
  0 siblings, 4 replies; 16+ messages in thread
From: Chris Hecker @ 2001-03-06  9:39 UTC (permalink / raw)
  To: caml-list


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.

Chris

-------------------
To unsubscribe, mail caml-list-request@inria.fr.  Archives: http://caml.inria.fr


^ permalink raw reply	[flat|nested] 16+ messages in thread

end of thread, other threads:[~2001-03-07 16:00 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-03-06 10:16 [Caml-list] currying Adam Granicz
  -- strict thread matches above, loose matches on Subject: below --
2001-03-06  9:39 Chris Hecker
2001-03-06 10:22 ` Thomas Colcombet
2001-03-06 10:33 ` Bruce Hoult
2001-03-06 10:50 ` Remi VANICAT
2001-03-06 16:31 ` Xavier Leroy
2001-03-06 17:41   ` Chris Hecker
2001-03-06 18:43     ` Sven LUTHER
2001-03-06 19:09     ` Marcin 'Qrczak' Kowalczyk
2001-03-06 20:14       ` Chris Hecker
2001-03-06 21:39         ` Marcin 'Qrczak' Kowalczyk
2001-03-06 23:23           ` Chris Hecker
2001-03-06 23:45             ` Marcin 'Qrczak' Kowalczyk
2001-03-07  1:10               ` Chris Hecker
2001-03-07  8:44                 ` Marcin 'Qrczak' Kowalczyk
2001-03-06 23:51             ` Chris Hecker

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).