Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to apply a function in Scheme to a list of arguments returned by another function?

Suppose there are two functions, f and v. Assume further that v returns a list of length n and f expects exactly n arguments. I am looking for the correct syntax in Scheme for applying f to the list returned by v.

If I use the syntax (f (v v-arguments)) then I get an error about f expectsing n arguments but receiving only one argument (which is the list returned by v).

If I use the syntax (f . (v v-arguments)), then the problem is too many arguments passed to f.

The best I could do (for the case when f expects two arguments) is this:

(let ((output-of-v (v v-arguments)))
  (f (car output-of-v) (cadr output-of-v)))

I am sure there must be a better way and I would be grateful for any advice!

like image 689
alex Avatar asked Dec 27 '22 17:12

alex


1 Answers

It seems you're looking for apply:

(apply f (v v-arguments))

As explained here, the apply function applies a function to a list of arguments, effectively passing them as positional arguments to that function.

like image 152
Frédéric Hamidi Avatar answered May 09 '23 04:05

Frédéric Hamidi