So, I am trying to see how functions that can accept any number of arguments work?
I tried this
(define (plus x . xs)
(if
(null? xs) x
(plus (+ x (car xs)) . (cdr xs))))
(plus 1 2 3 4)
But is seemed that it wasn't actually applying cdr to xs, but passing ( (2 3 4)) in when I stepped through it in the debugger. So I tried this
(define (plus* x . xs)
(if
(null? xs) x
(let ((h (car xs))
(t (crd xs)))
(plus* (+ x h) . t))))
Thinking: "ha, I'd like to see you pass cdr in now", but I get an error: "application: bad syntax (illegal use of `.') in: (plus* (+ x h) . t)"
What is going on?
(I can get a version of addition to work, either by
(define (add . xs)
(foldl + 0 xs))
Or even
(define (plus x . xs)
(if
(null? xs) x
(apply plus (cons (+ x (car xs)) (cdr xs)))))
so, the addition is not the problem, how dotted things work is.)