add pic_reverse

This commit is contained in:
Yuichi Nishiwaki 2013-10-22 00:19:43 +09:00
parent 114d6b708b
commit 0c1babb6c5
3 changed files with 14 additions and 9 deletions

View File

@ -8,6 +8,8 @@ pic_value pic_cdr(pic_state *, pic_value);
bool pic_list_p(pic_state *, pic_value); bool pic_list_p(pic_state *, pic_value);
pic_value pic_list(pic_state *, size_t, ...); pic_value pic_list(pic_state *, size_t, ...);
pic_value pic_reverse(pic_state *, pic_value);
pic_value pic_assq(pic_state *, pic_value key, pic_value assoc); pic_value pic_assq(pic_state *, pic_value key, pic_value assoc);
pic_value pic_acons(pic_state *, pic_value key, pic_value val, pic_value assoc); pic_value pic_acons(pic_state *, pic_value key, pic_value val, pic_value assoc);

View File

@ -389,21 +389,13 @@ pic_gen(pic_state *pic, struct pic_irep *irep, pic_value obj, struct pic_env *en
} }
} }
static pic_value
reverse(pic_state *pic, pic_value list, pic_value acc)
{
if (pic_nil_p(list))
return acc;
return reverse(pic, pic_cdr(pic, list), pic_cons(pic, pic_car(pic, list), acc));
}
static void static void
pic_gen_call(pic_state *pic, struct pic_irep *irep, pic_value obj, struct pic_env *env) pic_gen_call(pic_state *pic, struct pic_irep *irep, pic_value obj, struct pic_env *env)
{ {
pic_value seq; pic_value seq;
int i = 0; int i = 0;
seq = reverse(pic, obj, pic_nil_value()); seq = pic_reverse(pic, obj);
for (; ! pic_nil_p(seq); seq = pic_cdr(pic, seq)) { for (; ! pic_nil_p(seq); seq = pic_cdr(pic, seq)) {
pic_value v; pic_value v;

View File

@ -48,6 +48,17 @@ pic_list_p(pic_state *pic, pic_value obj)
return pic_nil_p(obj); return pic_nil_p(obj);
} }
pic_value
pic_reverse(pic_state *pic, pic_value list)
{
pic_value v, acc = pic_nil_value();
for (v = list; ! pic_nil_p(v); v = pic_cdr(pic ,v)) {
acc = pic_cons(pic, pic_car(pic, v), acc);
}
return acc;
}
pic_value pic_value
pic_assq(pic_state *pic, pic_value key, pic_value assoc) pic_assq(pic_state *pic, pic_value key, pic_value assoc)
{ {