+,-,*,/: take n arguments.
This commit is contained in:
parent
7f35686b61
commit
3a29b03529
55
mes.c
55
mes.c
|
@ -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 *
|
||||||
|
|
2
scm.mes
2
scm.mes
|
@ -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)))
|
||||||
|
|
6
test.mes
6
test.mes
|
@ -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)))
|
||||||
|
|
Loading…
Reference in a new issue