caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* How to add () to function parameters
@ 2009-10-14 10:44 Till Crueger
  2009-10-16 15:15 ` [Caml-list] " Till Varoquaux
  0 siblings, 1 reply; 3+ messages in thread
From: Till Crueger @ 2009-10-14 10:44 UTC (permalink / raw)
  To: caml-list

Hi,

I am looking for a way to add a unit parameter to a function that takes an  
arbitrary number of parameters. If the number of parameters is known this  
is fairly easy and I can just do:

let lift1 f a =
    fun () ->
       f a;;

let lift2 f a b =
    fun () ->
      f a b;;

(all these create one closure per lifting)
etc...

However it is a bit of a hassle to have to code each of these lifts... So  
what I am looking for is a way to extend this pattern to all numbers. So  
far I got to the point that I can do the following:

let lift_once f a =
    fun () ->
       f a;;

let lift_more f a =
    fun () ->
       f () a;;

So for a function f taking two parameters a and b I can do

lift_more (lift_once f a) b
(two closures created)

and for a function taking the parameters a, b and c I can do

lift_more (lift_more (lift_once f a) b) c
(three closures created)

to get the lifted functions.

However this solution gets quite ugly with all the parentheses. Also there  
are a lot of closures being produced and evaluated for any single lifting.  
I had a look at the Jane Street blog post about variable argument  
functions (http://ocaml.janestcapital.com/?q=node/22), which seems to do  
similar things. However I have never been really good with CPS, so I don't  
know if those techniques can be applied to this problem.

Is there any way to do this, which does not get this ugly. Also the  
resulting lifted function should not contain too many closures.

Thanks for your help,
    Till


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

end of thread, other threads:[~2009-10-16 15:22 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-10-14 10:44 How to add () to function parameters Till Crueger
2009-10-16 15:15 ` [Caml-list] " Till Varoquaux
2009-10-16 15:22   ` Till Varoquaux

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