Browse thread
[Caml-list] Function call with a list of parameters
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: | 2001-12-11 (23:26) |
From: | Bruce Hoult <bruce@h...> |
Subject: | Re: [Caml-list] Function call with a list of parameters |
At 2:59 PM -0800 11/12/01, Chris Hecker wrote: > >I'm trying to construct a function which take two arguments : >> Arg1 : a function, Arg2 : a list of parameters for the Arg1. >>This function will call the function in Arg1 with Arg2 as parameters. > >This is slightly related to a feature I'd like that's easy to do in >lisp, but I don't think there's a way to do it in ML-style languages: > >I have a function that returns a tuple, and a function that takes >two curried parameters. I'd like to pass the results of the first >to the second, without having to break up the tuple with fst and snd >(or pattern matching). > >let f () = (1,2) >let g x y = x + y > >g (? f ()) > >vs. > >let x,y = f () in >g x y > >With lisp you can just "apply" and it works. There's no way in caml >to spread the arguments into a curried function application, however. No, that's not the case. "Apply" needs a list, but multiple valued function result is NOT a list. It will be treated by "apply" as being just the first value. bruce@k7:~ > cmucl CMU Common Lisp 18c, running on k7 Send questions and bug reports to your local CMU CL maintainer, or to cmucl-help@cons.org. and cmucl-imp@cons.org. respectively. Loaded subsystems: Python 1.0, target Intel x86 CLOS based on PCL version: September 16 92 PCL (f) * (defun f () (values 1 2)) F * (defun g (x y) (+ x y)) G * (apply #'g '(1 2)) 3 * (apply #'g (f)) Type-error in KERNEL::OBJECT-NOT-LIST-ERROR-HANDLER: 1 is not of type LIST I'm not quite sure off the top of my head the correct thing to do in Common Lisp, but it will be similar to the Dylan: define function f() values(1, 2) end; define function g(x, y) x + y end; let (#rest results) = f(); apply(g, results); Ah, here we go, in Common Lisp. Either... (apply #'g (multiple-value-list (f))) ... or ... (multiple-value-call #'g (f)) Hope this helps. -- Bruce ------------------- Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr