From 7865cfe9b3f20a8e47117d1fc075f0e2ad7642ce Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Wed, 22 Jan 2014 20:57:14 +0900 Subject: [PATCH] add pic_append --- include/picrin/pair.h | 1 + src/pair.c | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/include/picrin/pair.h b/include/picrin/pair.h index 02599ff6..f2ba887e 100644 --- a/include/picrin/pair.h +++ b/include/picrin/pair.h @@ -19,6 +19,7 @@ pic_value pic_list_from_array(pic_state *, size_t, pic_value *); int pic_length(pic_state *, pic_value); pic_value pic_reverse(pic_state *, pic_value); +pic_value pic_append(pic_state *, pic_value, pic_value); pic_value pic_assq(pic_state *, pic_value key, pic_value assoc); pic_value pic_assoc(pic_state *, pic_value key, pic_value assoc); diff --git a/src/pair.c b/src/pair.c index ee443f68..989af584 100644 --- a/src/pair.c +++ b/src/pair.c @@ -114,6 +114,23 @@ pic_reverse(pic_state *pic, pic_value list) return acc; } +pic_value +pic_append(pic_state *pic, pic_value xs, pic_value ys) +{ + int ai = pic_gc_arena_preserve(pic); + + if (pic_nil_p(xs)) { + return ys; + } + else { + xs = pic_cons(pic, pic_car(pic, xs), pic_append(pic, pic_cdr(pic, xs), ys)); + } + + pic_gc_arena_restore(pic, ai); + pic_gc_protect(pic, xs); + return xs; +} + pic_value pic_assq(pic_state *pic, pic_value key, pic_value assoc) {