Question

(define test (lambda args 
  (if (= (length args) 1) (display (car args))
      (begin (display (car args))
      (test (cdr args))))))

I was looking for it on the net and didnt find the answer, I was able to get a variable number of arguments to my func, but how do i pass them not as a single list to the next iteration?

To be more clearer, if i call :

(test (list 1 1) (list 2 2) (list 3 3))

i was exepcting to get :

(1 1) (2 2) (3 3)

but i get :

(1 1) ((2 2) (3 3))

Thanks in advance!

Was it helpful?

Solution

args will be a list of the arguments passed to test. The first time you pass it three lists, so args will be a list of those three lists. Recursively you pass it (cdr args), which is a list of the last two lists, so now args is a list containing the list that has the last two lists. That is, args is (((2 2) (3 3))) where you want ((2 2) (3 3)). You can fix it by applying the test function to the list:

(apply test (cdr args))

Which, if (cdr args) is ((2 2) (3 3)), is the same as doing:

(test (2 2) (3 3))

OTHER TIPS

(define (test . args)
  (if (= (length args) 1)
    (display (car args))
    (begin
      (display (car args))
      (apply test (cdr args)))))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top