Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] currying...
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Thomas Colcombet <Thomas.Colcombet@i...>
Subject: Re: [Caml-list] currying...
As I understand the problem, may be the small following examples explain
how CAML
implements curryfication :

# let f = fun x -> (Printf.printf "%d\n" x; fun y -> Printf.printf
"%d\n" y);;
val f : int -> int -> unit = <fun>
# f 1;;
1
- : int -> unit = <fun>
# f 1 2;;
1
2
- : unit = ()

This function does not behave as the following one :

# let f = fun x y -> (Printf.printf "%d\n" x; Printf.printf "%d\n" y);;
val f : int -> int -> unit = <fun>
# f 1;;
- : int -> unit = <fun>
# f 1 2;;
1
2
- : unit = ()

In other words, a fun construct is evaluated only when all its argument
are known.
The notation "let f x y = ..." is just syntactic sugar for "let f = fun
x y -> ...".

	Thomas Colcombet

> 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
-------------------
To unsubscribe, mail caml-list-request@inria.fr.  Archives: http://caml.inria.fr