Version française
Home     About     Download     Resources     Contact us    
Browse thread
function definition
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Vu Ngoc San <san.vu-ngoc@u...>
Subject: function definition
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