implement assoc and member in c
This commit is contained in:
parent
44e80e62f4
commit
3f6ec5f878
|
@ -51,10 +51,11 @@ pic_value pic_append(pic_state *, pic_value, pic_value);
|
||||||
|
|
||||||
pic_value pic_memq(pic_state *, pic_value key, pic_value list);
|
pic_value pic_memq(pic_state *, pic_value key, pic_value list);
|
||||||
pic_value pic_memv(pic_state *, pic_value key, pic_value list);
|
pic_value pic_memv(pic_state *, pic_value key, pic_value list);
|
||||||
|
pic_value pic_member(pic_state *, pic_value key, pic_value list, struct pic_proc * /* = NULL */);
|
||||||
|
|
||||||
pic_value pic_assq(pic_state *, pic_value key, pic_value assoc);
|
pic_value pic_assq(pic_state *, pic_value key, pic_value assoc);
|
||||||
pic_value pic_assv(pic_state *, pic_value key, pic_value assoc);
|
pic_value pic_assv(pic_state *, pic_value key, pic_value assoc);
|
||||||
pic_value pic_assoc(pic_state *, pic_value key, pic_value assoc);
|
pic_value pic_assoc(pic_state *, pic_value key, pic_value assoc, struct pic_proc * /* = NULL */);
|
||||||
|
|
||||||
pic_value pic_acons(pic_state *, pic_value key, pic_value val, pic_value assoc);
|
pic_value pic_acons(pic_state *, pic_value key, pic_value val, pic_value assoc);
|
||||||
|
|
||||||
|
|
|
@ -589,26 +589,6 @@
|
||||||
s
|
s
|
||||||
(fold f (f (car xs) s) (cdr xs))))
|
(fold f (f (car xs) s) (cdr xs))))
|
||||||
|
|
||||||
;;; 6.4 Pairs and lists
|
|
||||||
|
|
||||||
(define (member obj list . opts)
|
|
||||||
(let ((compare (if (null? opts) equal? (car opts))))
|
|
||||||
(if (null? list)
|
|
||||||
#f
|
|
||||||
(if (compare obj (car list))
|
|
||||||
list
|
|
||||||
(member obj (cdr list) compare)))))
|
|
||||||
|
|
||||||
(define (assoc obj list . opts)
|
|
||||||
(let ((compare (if (null? opts) equal? (car opts))))
|
|
||||||
(if (null? list)
|
|
||||||
#f
|
|
||||||
(if (compare obj (caar list))
|
|
||||||
(car list)
|
|
||||||
(assoc obj (cdr list) compare)))))
|
|
||||||
|
|
||||||
(export member assoc)
|
|
||||||
|
|
||||||
;;; 6.6 Characters
|
;;; 6.6 Characters
|
||||||
|
|
||||||
(define-macro (define-char-transitive-predicate name op)
|
(define-macro (define-char-transitive-predicate name op)
|
||||||
|
|
|
@ -54,7 +54,7 @@ pic_find_library(pic_state *pic, pic_value spec)
|
||||||
{
|
{
|
||||||
pic_value v;
|
pic_value v;
|
||||||
|
|
||||||
v = pic_assoc(pic, spec, pic->lib_tbl);
|
v = pic_assoc(pic, spec, pic->lib_tbl, NULL);
|
||||||
if (pic_false_p(v)) {
|
if (pic_false_p(v)) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
51
src/pair.c
51
src/pair.c
|
@ -291,6 +291,26 @@ pic_memv(pic_state *pic, pic_value key, pic_value list)
|
||||||
goto enter;
|
goto enter;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pic_value
|
||||||
|
pic_member(pic_state *pic, pic_value key, pic_value list, struct pic_proc *compar)
|
||||||
|
{
|
||||||
|
enter:
|
||||||
|
|
||||||
|
if (pic_nil_p(list))
|
||||||
|
return pic_false_value();
|
||||||
|
|
||||||
|
if (compar == NULL) {
|
||||||
|
if (pic_equal_p(pic, key, pic_car(pic, list)))
|
||||||
|
return list;
|
||||||
|
} else {
|
||||||
|
if (pic_test(pic_apply2(pic, compar, key, pic_car(pic, list))))
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
list = pic_cdr(pic, list);
|
||||||
|
goto enter;
|
||||||
|
}
|
||||||
|
|
||||||
pic_value
|
pic_value
|
||||||
pic_assq(pic_state *pic, pic_value key, pic_value assoc)
|
pic_assq(pic_state *pic, pic_value key, pic_value assoc)
|
||||||
{
|
{
|
||||||
|
@ -328,7 +348,7 @@ pic_assv(pic_state *pic, pic_value key, pic_value assoc)
|
||||||
}
|
}
|
||||||
|
|
||||||
pic_value
|
pic_value
|
||||||
pic_assoc(pic_state *pic, pic_value key, pic_value assoc)
|
pic_assoc(pic_state *pic, pic_value key, pic_value assoc, struct pic_proc *compar)
|
||||||
{
|
{
|
||||||
pic_value cell;
|
pic_value cell;
|
||||||
|
|
||||||
|
@ -338,8 +358,13 @@ pic_assoc(pic_state *pic, pic_value key, pic_value assoc)
|
||||||
return pic_false_value();
|
return pic_false_value();
|
||||||
|
|
||||||
cell = pic_car(pic, assoc);
|
cell = pic_car(pic, assoc);
|
||||||
|
if (compar == NULL) {
|
||||||
if (pic_equal_p(pic, key, pic_car(pic, cell)))
|
if (pic_equal_p(pic, key, pic_car(pic, cell)))
|
||||||
return cell;
|
return cell;
|
||||||
|
} else {
|
||||||
|
if (pic_test(pic_apply2(pic, compar, key, pic_car(pic, cell))))
|
||||||
|
return cell;
|
||||||
|
}
|
||||||
|
|
||||||
assoc = pic_cdr(pic, assoc);
|
assoc = pic_cdr(pic, assoc);
|
||||||
goto enter;
|
goto enter;
|
||||||
|
@ -662,6 +687,17 @@ pic_pair_memv(pic_state *pic)
|
||||||
return pic_memv(pic, key, list);
|
return pic_memv(pic, key, list);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static pic_value
|
||||||
|
pic_pair_member(pic_state *pic)
|
||||||
|
{
|
||||||
|
struct pic_proc *proc = NULL;
|
||||||
|
pic_value key, list;
|
||||||
|
|
||||||
|
pic_get_args(pic, "oo|l", &key, &list, &proc);
|
||||||
|
|
||||||
|
return pic_member(pic, key, list, proc);
|
||||||
|
}
|
||||||
|
|
||||||
static pic_value
|
static pic_value
|
||||||
pic_pair_assq(pic_state *pic)
|
pic_pair_assq(pic_state *pic)
|
||||||
{
|
{
|
||||||
|
@ -682,6 +718,17 @@ pic_pair_assv(pic_state *pic)
|
||||||
return pic_assv(pic, key, list);
|
return pic_assv(pic, key, list);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static pic_value
|
||||||
|
pic_pair_assoc(pic_state *pic)
|
||||||
|
{
|
||||||
|
struct pic_proc *proc = NULL;
|
||||||
|
pic_value key, list;
|
||||||
|
|
||||||
|
pic_get_args(pic, "oo|l", &key, &list, &proc);
|
||||||
|
|
||||||
|
return pic_assoc(pic, key, list, proc);
|
||||||
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
pic_init_pair(pic_state *pic)
|
pic_init_pair(pic_state *pic)
|
||||||
{
|
{
|
||||||
|
@ -708,6 +755,8 @@ pic_init_pair(pic_state *pic)
|
||||||
pic_defun(pic, "list-copy", pic_pair_list_copy);
|
pic_defun(pic, "list-copy", pic_pair_list_copy);
|
||||||
pic_defun(pic, "memq", pic_pair_memq);
|
pic_defun(pic, "memq", pic_pair_memq);
|
||||||
pic_defun(pic, "memv", pic_pair_memv);
|
pic_defun(pic, "memv", pic_pair_memv);
|
||||||
|
pic_defun(pic, "member", pic_pair_member);
|
||||||
pic_defun(pic, "assq", pic_pair_assq);
|
pic_defun(pic, "assq", pic_pair_assq);
|
||||||
pic_defun(pic, "assv", pic_pair_assv);
|
pic_defun(pic, "assv", pic_pair_assv);
|
||||||
|
pic_defun(pic, "assoc", pic_pair_assoc);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue