2
(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!

Ofek Ron
  • 8,354
  • 13
  • 55
  • 103

2 Answers2

2
(define (test . args)
  (if (= (length args) 1)
    (display (car args))
    (begin
      (display (car args))
      (apply test (cdr args)))))
to_the_crux
  • 257
  • 1
  • 6
  • what does the dot mean? and what does apply do? – Ofek Ron Jan 30 '13 at 18:47
  • The dot means that the next parameter will be given a list that holds all of the remaining arguments to the function. "apply" is used to break up a list into individual arguments to a function; (apply + '(1 3 5 7)) is equivalent to (+ 1 3 5 7); "apply" is also found in Lisp. Both are standard, basic Scheme. – to_the_crux Jan 31 '13 at 06:16
2

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))
Michael Mrozek
  • 169,610
  • 28
  • 168
  • 175