+,-,*,/: 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 *
minus (scm *a, scm *b)
minus (scm *x/*...*/)
{
scm *a = car (x);
assert (a->type == NUMBER);
assert (b->type == NUMBER);
return make_number (a->value - b->value);
int n = a->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 *
plus (scm *a, scm *b)
plus (scm *x/*...*/)
{
assert (a->type == NUMBER);
assert (b->type == NUMBER);
return make_number (a->value + b->value);
int n = 0;
while (x != &scm_nil)
{
assert (x->car->type == NUMBER);
n += x->car->value;
x = cdr (x);
}
return make_number (n);
}
scm *
divide (scm *a, scm *b)
divide (scm *x/*...*/)
{
assert (a->type == NUMBER);
assert (b->type == NUMBER);
return make_number (a->value / b->value);
int n = 1;
while (x != &scm_nil)
{
assert (x->car->type == NUMBER);
n /= x->car->value;
x = cdr (x);
}
return make_number (n);
}
scm *
multiply (scm *a, scm *b)
multiply (scm *x/*...*/)
{
assert (a->type == NUMBER);
assert (b->type == NUMBER);
return make_number (a->value * b->value);
int n = 1;
while (x != &scm_nil)
{
assert (x->car->type == NUMBER);
n *= x->car->value;
x = cdr (x);
}
return make_number (n);
}
scm *

View file

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

View file

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