List Info

Thread: "ocaml_beginners"::[] returning a closure - why and why not?




"ocaml_beginners"::[] returning a closure - why and why not?
country flaguser name
United States
2008-03-02 19:23:54

In article
http://groups.google.com/group/comp.lang.functional/browse_thread/thread/a288a989c5cd6acb/50b6ca2607173c91
Jon Harrop writes

let invoke (f : 'a -> 'b) x : unit -> 'b =
...[snipped]...

It has this signature
val invoke : ('a -> 'b) -> 'a -> unit -> 'b

I am puzzled why the above signature is preferable to the
simpler one below? He must have had a good reason to include 'unit'

val invoke: ('a -> 'b) -> 'a -> 'b
(* I'd rather define it like this with my limited understanding *)

In general in what circumstances is it desirable to return closures
from functions? "Because you can" is not a satisfactory answer.

__._,_.___
.

__,_._,___
Re: "ocaml_beginners"::[] returning a closure - why and why not?
country flaguser name
United Kingdom
2008-03-03 03:16:06

On Monday 03 March 2008 01:23:54 tenuc70 wrote:
> In article
> http://groups.google.com/group/comp.lang.functional/browse_thread/thread/a2
>;88a989c5cd6acb/50b6ca2607173c91 Jon Harrop writes
>
> let invoke (f : 'a -> 'b) x : unit -> 'b =
> ...[snipped]...
>
> It has this signature
> val invoke : ('a -> 'b) -> 'a -> unit -> 'b
>
> I am puzzled why the above signature is preferable to the
> simpler one below? He must have had a good reason to include 'unit'
>
> val invoke: ('a -> 'b) -> 'a -> 'b
> (* I'd rather define it like this with my limited understanding *)
>
> In general in what circumstances is it desirable to return closures
> from functions? "Because you can" is not a satisfactory answer.

Currying is usually used because you want to partially apply your function. In
that case, I used currying because I wanted application of the argument to
start the concurrent thread of execution and application of the subsequent
value of the type unit to block until the concurrent execution was complete,
extract the result and return it.

So the sole purpose of that "invoke" function is that:

let future = invoke f x
...
let f_x = future()

executes "f" concurrently with "...".

--
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
http://www.ffconsultancy.com/products/?e

__._,_.___
.

__,_._,___
[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )