assq: use while instead of recursion.

This commit is contained in:
Jan Nieuwenhuizen 2016-10-08 08:47:23 +02:00
parent 509bf3956e
commit e504998b03

22
mes.c
View file

@ -142,15 +142,18 @@ cons (scm *x, scm *y)
return p; return p;
} }
#define EQ_P(x, y)\
((x == y \
|| (x->type == CHAR && y->type == CHAR \
&& x->value == y->value) \
|| (x->type == NUMBER && y->type == NUMBER \
&& x->value == y->value)) \
? &scm_t : &scm_f)
scm * scm *
eq_p (scm *x, scm *y) eq_p (scm *x, scm *y)
{ {
return (x == y return EQ_P (x, y);
|| (x->type == CHAR && y->type == CHAR
&& x->value == y->value)
|| (x->type == NUMBER && y->type == NUMBER
&& x->value == y->value))
? &scm_t : &scm_f;
} }
scm * scm *
@ -162,7 +165,7 @@ macro_p (scm *x)
scm * scm *
null_p (scm *x) null_p (scm *x)
{ {
return eq_p (x, &scm_nil); return x == &scm_nil ? &scm_t : &scm_f;
} }
scm * scm *
@ -285,15 +288,14 @@ pairlis (scm *x, scm *y, scm *a)
scm * scm *
assq (scm *x, scm *a) assq (scm *x, scm *a)
{ {
while (a != &scm_nil && EQ_P (x, a->car->car) == &scm_f) a = a->cdr;
if (a == &scm_nil) { if (a == &scm_nil) {
#if DEBUG #if DEBUG
printf ("alist miss: %s\n", x->name); printf ("alist miss: %s\n", x->name);
#endif #endif
return &scm_f; return &scm_f;
} }
if (eq_p (caar (a), x) == &scm_t) return a->car;
return car (a);
return assq (x, cdr (a));
} }
scm * scm *