caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* function definition
@ 2007-01-21 12:45 Vu Ngoc San
  2007-01-21 17:42 ` [Caml-list] " Jon Harrop
  0 siblings, 1 reply; 3+ messages in thread
From: Vu Ngoc San @ 2007-01-21 12:45 UTC (permalink / raw)
  To: Caml Mailing List

I'm sure this is a basic question:

what is the difference between these ways of defining a function, and 
what is the most efficient (I mean for the resulting function f = binop 
o f1 f2, which typically will be called x*1000 times)

type operator = Plus | Minus;;


let binop1 o f1 f2 =
   fun x -> match o with
	| Plus -> (f1 x) +. (f2 x)
	| Minus -> (f1 x) -. (f2 x)

let binop2 o f1 f2 =
   match o with
	| Plus -> fun x -> (f1 x) +. (f2 x)
	| Minus -> fun x -> (f1 x) -. (f2 x)

let binop3 o f1 f2 =
   let op = match o with
	| Plus ->  (+.)
	| Minus -> (-.) in
	fun x -> op (f1 x) (f2 x)

let binop4 o f1 f2 =
   fun x ->
     let op = match o with
	| Plus ->  (+.)
	| Minus -> (-.) in
       	op (f1 x) (f2 x)

Thanks for your expertise !

San



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

end of thread, other threads:[~2007-01-23 11:44 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-01-21 12:45 function definition Vu Ngoc San
2007-01-21 17:42 ` [Caml-list] " Jon Harrop
2007-01-23 11:44   ` Vu Ngoc San

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