From aac2082991122e0613fa719b9ca7cf67eeedb2bf Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sat, 7 Dec 2013 06:30:21 -0800 Subject: [PATCH] add pic_assoc --- include/picrin/pair.h | 1 + src/pair.c | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/include/picrin/pair.h b/include/picrin/pair.h index 5fd18e62..0167f96c 100644 --- a/include/picrin/pair.h +++ b/include/picrin/pair.h @@ -13,6 +13,7 @@ int pic_length(pic_state *, pic_value); pic_value pic_reverse(pic_state *, 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); pic_value pic_acons(pic_state *, pic_value key, pic_value val, pic_value assoc); pic_value pic_caar(pic_state *, pic_value); diff --git a/src/pair.c b/src/pair.c index 193d4c77..83ac92d6 100644 --- a/src/pair.c +++ b/src/pair.c @@ -128,6 +128,24 @@ pic_assq(pic_state *pic, pic_value key, pic_value assoc) goto enter; } +pic_value +pic_assoc(pic_state *pic, pic_value key, pic_value assoc) +{ + pic_value cell; + + enter: + + if (pic_nil_p(assoc)) + return assoc; + + cell = pic_car(pic, assoc); + if (pic_equal_p(pic, key, pic_car(pic, cell))) + return cell; + + assoc = pic_cdr(pic, assoc); + goto enter; +} + pic_value pic_acons(pic_state *pic, pic_value key, pic_value val, pic_value assoc) {