+,-,*,/: take n arguments.

This commit is contained in:
Jan Nieuwenhuizen 2016-07-11 20:58:30 +02:00
parent 7f35686b61
commit 3a29b03529
3 changed files with 45 additions and 18 deletions

55
mes.c
View File

@ -971,35 +971,60 @@ less_p (scm *a, scm *b)
} }
scm * scm *
minus (scm *a, scm *b) minus (scm *x/*...*/)
{ {
scm *a = car (x);
assert (a->type == NUMBER); assert (a->type == NUMBER);
assert (b->type == NUMBER); int n = a->value;
return make_number (a->value - b->value); x = cdr (x);
if (x == &scm_nil)
n = -n;
while (x != &scm_nil)
{
assert (x->car->type == NUMBER);
n -= x->car->value;
x = cdr (x);
}
return make_number (n);
} }
scm * scm *
plus (scm *a, scm *b) plus (scm *x/*...*/)
{ {
assert (a->type == NUMBER); int n = 0;
assert (b->type == NUMBER); while (x != &scm_nil)
return make_number (a->value + b->value); {
assert (x->car->type == NUMBER);
n += x->car->value;
x = cdr (x);
}
return make_number (n);
} }
scm * scm *
divide (scm *a, scm *b) divide (scm *x/*...*/)
{ {
assert (a->type == NUMBER); int n = 1;
assert (b->type == NUMBER); while (x != &scm_nil)
return make_number (a->value / b->value); {
assert (x->car->type == NUMBER);
n /= x->car->value;
x = cdr (x);
}
return make_number (n);
} }
scm * scm *
multiply (scm *a, scm *b) multiply (scm *x/*...*/)
{ {
assert (a->type == NUMBER); int n = 1;
assert (b->type == NUMBER); while (x != &scm_nil)
return make_number (a->value * b->value); {
assert (x->car->type == NUMBER);
n *= x->car->value;
x = cdr (x);
}
return make_number (n);
} }
scm * scm *

View File

@ -33,8 +33,6 @@
(#t (memq x (cdr lst))))) (#t (memq x (cdr lst)))))
(define memv memq) (define memv memq)
(define (+ x y) (- x (- 0 y)))
(define-macro (and x y) (define-macro (and x y)
(cond (x y) (cond (x y)
(#t #f))) (#t #f)))

View File

@ -238,6 +238,10 @@
(display (memq 'd '(a b c))) (display (memq 'd '(a b c)))
(newline) (newline)
(display "plus: ")
(display (+ 1 1 1 1))
(newline)
(cond ((defined? 'loop2) (cond ((defined? 'loop2)
(display "mes:values broken after loop2") (display "mes:values broken after loop2")
(newline)) (newline))
@ -250,7 +254,7 @@
(display "call-with-values ==> 6: ") (display "call-with-values ==> 6: ")
(display (display
(call-with-values (lambda () (values 1 2 3)) (call-with-values (lambda () (values 1 2 3))
(lambda (a b c) (+ (+ a b) c)))) (lambda (a b c) (+ a b c))))
(newline) (newline)
(display "call-with-values ==> 1: ") (display "call-with-values ==> 1: ")
(display ((lambda (x) x) (values 1 2 3))) (display ((lambda (x) x) (values 1 2 3)))