diff --git a/src/pair.c b/src/pair.c index 80ac57a4..e461ba70 100644 --- a/src/pair.c +++ b/src/pair.c @@ -138,17 +138,17 @@ pic_value pic_append(pic_state *pic, pic_value xs, pic_value ys) { int ai = pic_gc_arena_preserve(pic); + pic_value x; - if (pic_nil_p(xs)) { - return ys; - } - else { - xs = pic_cons(pic, pic_car(pic, xs), pic_append(pic, pic_cdr(pic, xs), ys)); - } + xs = pic_reverse(pic, xs); + pic_for_each (x, xs) { + ys = pic_cons(pic, x, ys); - pic_gc_arena_restore(pic, ai); - pic_gc_protect(pic, xs); - return xs; + pic_gc_arena_restore(pic, ai); + pic_gc_protect(pic, xs); + pic_gc_protect(pic, ys); + } + return ys; } pic_value