From a443d9e3f6be9d45fb3443401d5940fb5cc67613 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Wed, 22 Jan 2014 20:57:05 +0900 Subject: [PATCH] add pic_list_ref and pic_list_tail --- include/picrin/pair.h | 3 +++ src/pair.c | 15 +++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/include/picrin/pair.h b/include/picrin/pair.h index 1aed0d98..02599ff6 100644 --- a/include/picrin/pair.h +++ b/include/picrin/pair.h @@ -29,6 +29,9 @@ pic_value pic_cadr(pic_state *, pic_value); pic_value pic_cdar(pic_state *, pic_value); pic_value pic_cddr(pic_state *, pic_value); +pic_value pic_list_tail(pic_state *, pic_value ,int); +pic_value pic_list_ref(pic_state *, pic_value, int); + #if defined(__cplusplus) } #endif diff --git a/src/pair.c b/src/pair.c index e6762221..ee443f68 100644 --- a/src/pair.c +++ b/src/pair.c @@ -180,6 +180,21 @@ pic_cddr(pic_state *pic, pic_value v) return pic_cdr(pic, pic_cdr(pic, v)); } +pic_value +pic_list_tail(pic_state *pic, pic_value list, int i) +{ + while (i-- > 0) { + list = pic_cdr(pic, list); + } + return list; +} + +pic_value +pic_list_ref(pic_state *pic, pic_value list, int i) +{ + return pic_car(pic, pic_list_tail(pic, list, i)); +} + static pic_value pic_pair_pair_p(pic_state *pic) {