Thanks Jon, great example of concurrent execution by the way.
I was already using currying with the reduced signature as shown below
but your explanation is perfect. Also I found your usage of the
'ignore' function from pervasives very useful.
Here is the conclusion that I draw from this discussion:
declarations like
let func x y () = x + y
are less desirable than
let func x y = fun () -> x + y
for stylistic reasons although they are equivalent.
'unit' can also be used for disambiguation when optional arguments are
declared.
let _ =
let rstr = invoke string_of_int 5
and rint = invoke int_of_string "134"
and radd = invoke (fun (x, y) -> x +. y) (3.4, 4.5)
and rsum = invoke (List.fold_right (fun x y -> y + (int_of_string
x)) ["1"; "5"; "6"; "8"; "10"]) 0 (*currying here *) in
Printf.printf "result = %s %d %f sum=%dn" rstr rint radd rsum
--- In ocaml_beginners%40yahoogroups.com">ocaml_beginners
yahoogroups.com, Jon Harrop <jon
...> wrote:
>
> 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
>
.