4

I have the following variadic function (define doSomething (lambda (x . rest) .... The function is called by using numbers, for example: (doSomething 1 2 3 4 5) (so with that call x would be 1 and rest would be (2 3 4 5)).

When I try to recursively call the function and put the second number (2) as x and rest as (3 4 5) I somehow receive the rest parameter as a list of list: ((3 4 5)).

This is how I currently try to call the function again: (+ x (doSomething (car rest) (cdr rest)))

It is worth mentioning that I'm using Pretty Big. Please advise, thanks.

1 Answer 1

2

So you're mix and matching what rest is, in your first call

(doSomething 1 2 3 4 5)   ; x = 1  rest = '(2 3 4 5)

In your subsequent calls you'll end up with

(doSomething (car rest) (cdr rest))   ; x=2  rest = '((3 4 5))

because rest is a variadic argument, so it takes everything after the first argument and makes it a list called rest for you, hence the double-listing. You'll probably want to be using apply or something, ie something like:

(define doSomething (lambda (x . rest) 
                      (display x) 
                      (if (not (null? rest))
                        (apply doSomething rest)
                        #f)))
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.