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) {