From 19fb18a343d88621ebab989539a3463b1ffbba7d Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Tue, 24 Jan 2017 10:44:10 -0800 Subject: New memp function for searching a plist. * eval.c (eval_init): Register memp intrinsic. * lib.c (memp): New function. * lib.h (memp): Declared. * txr.1: Documented. --- eval.c | 1 + lib.c | 9 +++++++++ lib.h | 1 + txr.1 | 48 +++++++++++++++++++++++++++++++++++++++++++----- 4 files changed, 54 insertions(+), 5 deletions(-) diff --git a/eval.c b/eval.c index e21ec820..8adea078 100644 --- a/eval.c +++ b/eval.c @@ -5800,6 +5800,7 @@ void eval_init(void) reg_fun(intern(lit("copy-cons"), user_package), func_n1(copy_cons)); reg_fun(intern(lit("copy-alist"), user_package), func_n1(copy_alist)); reg_fun(intern(lit("prop"), user_package), func_n2(getplist)); + reg_fun(intern(lit("memp"), user_package), func_n2(memp)); reg_fun(intern(lit("merge"), user_package), func_n4o(merge_wrap, 2)); reg_fun(intern(lit("sort"), user_package), func_n3o(sort, 1)); reg_fun(intern(lit("shuffle"), user_package), func_n1(shuffle)); diff --git a/lib.c b/lib.c index fdba27d2..40cae588 100644 --- a/lib.c +++ b/lib.c @@ -2855,6 +2855,15 @@ val getplist_f(val list, val key, loc found) return nil; } +val memp(val key, val plist) +{ + for (; plist; plist = cddr(plist)) { + if (car(plist) == key) + return plist; + } + return nil; +} + val proper_plist_to_alist(val list) { list_collect_decl (out, ptail); diff --git a/lib.h b/lib.h index 425acd01..80163f3c 100644 --- a/lib.h +++ b/lib.h @@ -611,6 +611,7 @@ val proper_list_p(val obj); val length_list(val list); val getplist(val list, val key); val getplist_f(val list, val key, loc found); +val memp(val key, val plist); val proper_plist_to_alist(val list); val improper_plist_to_alist(val list, val boolean_keys); val num(cnum val); diff --git a/txr.1 b/txr.1 index dd7b816e..198cca4b 100644 --- a/txr.1 +++ b/txr.1 @@ -18184,16 +18184,16 @@ function applied to the corresponding element of the input list. .SS* Property Lists -.coNP Function @ prop -.synb -.mets (prop < plist << key ) -.syne -.desc A property list a flat list of even length consisting of interleaved pairs of property names (usually symbols) and their values (arbitrary objects). An example property list is (:a 1 :b "two") which contains two properties, :a having value 1, and :b having value "two". +.coNP Function @ prop +.synb +.mets (prop < plist << key ) +.syne +.desc The .code prop function searches property list @@ -18211,6 +18211,44 @@ found, or due to the property being present with a .code nil value. +The indicators in +.meta plist +are compared with +.meta key +using +.code eq +equality, allowing them to be symbols, characters or +.code fixnum +integers. + +.coNP Function @ memp +.synb +.mets (memp < key << plist ) +.syne +.desc +The +.code memp +function searches property list +.meta plist +for key +.metn key , +using +.code eq +equality. + +If the key is found, then the entire suffix of +.meta plist +beginning with the indicator is returned, such that the first +element of the returned list is +.meta key +and the second element is the property value. + +Note the reversed argument convention relative to the +.code prop +function, harmonizing with functions in the +.code member +family. + .SS* List Sorting Note: these functions operate on lists. The principal sorting function -- cgit v1.2.3