Optional Arguments and Points in the Scheme

So, I'm trying to understand how functions work that can take any number of arguments?

I tried this

(define (plus x . xs)
  (if 
   (null? xs) x
   (plus (+ x (car xs)) . (cdr xs))))
(plus 1 2 3 4)

But it seems that in fact he did not apply cdr to xs, but passed ((2 3 4)) when I went 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 would like you to go through cdr now," but I get the error message: "application: bad syntax (illegal use`. ') In: (plus * (+ xh). T)"

What's happening?

(I can get a version of the add-on to work, either

(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 adding is not a problem how point things work.)

+5
source share
1 answer

- plus - apply. (, , , foldl.) , , , - -.

+6

All Articles