List Info

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




Re: "ocaml_beginners"::[] returning a closure - why and why not?
country flaguser name
United States
2008-03-03 08:13:12

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_beginnersyahoogroups.com, Jon Harrop <jon...> wrote:
&gt;
> On Monday 03 March 2008 01:23:54 tenuc70 wrote:
&gt; > In article
> >
http://groups.google.com/group/comp.lang.functional/browse_thread/thread/a2
>; >88a989c5cd6acb/50b6ca2607173c91 Jon Harrop writes
&gt; >
>; > let invoke (f : 'a -> 'b) x : unit -> 'b =
> > ...[snipped]...
&gt; >
>; > 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'
&gt; >
>; > 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:
&gt;
> let future = invoke f x
> ...
> let f_x = future()
>
> executes "f&quot; concurrently with "...&quot;.
&gt;
> --
> Dr Jon D Harrop, Flying Frog Consultancy Ltd.
>; http://www.ffconsultancy.com/products/?e
>

__._,_.___
.

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

On Monday 03 March 2008 14:13:12 tenuc70 wrote:
&gt; Thanks Jon, great example of concurrent execution by the way.
>; I was already using currying with the reduced signature as shown below
&gt; 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.

I would (stylistically) prefer the former more concise definition but YMMV.

They are semantically equivalent but the difference can also affect
performance in the presence of partial application. Although closures are
typically slower to invoke, you can actually leverage this approach to hoist
computation and improve performance.

This was discussed in the OCaml Journal article on optimizing a bytecode
interpreter.

--
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 )