0

Can Scheme "splice" the result of values or a list as the arguments for a function call? For example (imagined syntax): (list 0 (values 1 2) (values 3 4) 5) would return (0 1 2 3 4 5). The apply function isn't very convenient, since it only "splices" its last argument.

1
  • list already exists (and doesn't work like this), and it looks like values just returns its arguments, so it isn't very clear what you are asking for. Commented Mar 18 at 12:10

1 Answer 1

1

apply is the tool for this. Indeed it only splices its last argument, but if you supply it only one argument then you can effectively splice at any position by building the argument list using whatever tool you like. The tool often used for this is "quasiquote" (`), combined with unquote (,) and unquote-splicing (,@).

For example, to construct your desired list, supposing that (1 2) and (3 4) were variables instead of constants that you could just inline:

(define xs '(1 2))
(define ys '(3 4))
(define args `(0 ,@xs ,@ys 5))

Then you could write (apply f args), with the same effect as (f 0 1 2 3 4 5). And of course you don't need the args variable - this is the same as writing

(apply f `(0 ,@xs ,@ys 5))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.