Here's a patch so that assq-ref and friends behave like hash-ref.  I
just added an optional default argument.  This should not break any
existing user code, but does make hash-ref and assq-ref
"interchangable".  I fixed the doc-string so it matches the patch

Thanks,

Clark

--- alist.c     Tue Jun 27 09:37:38 2000
+++ alist.c.new Thu Jul  6 11:45:14 2000
@@ -198,13 +198,14 @@
 
 
 
-SCM_DEFINE (scm_assq_ref, "assq-ref", 2, 0, 0,
-            (SCM alist, SCM key),
-           "@deffnx primitive assv-ref alist key\n"
-           "@deffnx primitive assoc-ref alist key\n"
+SCM_DEFINE (scm_assq_ref, "assq-ref", 2, 1, 0,
+            (SCM alist, SCM key, SCM dflt),
+           "@deffnx primitive assv-ref alist key default\n"
+           "@deffnx primitive assoc-ref alist key default\n"
            "Like @code{assq}, @code{assv} and @code{assoc}, except that only the\n"
-           "value associated with @var{key} in @var{alist} is returned.  These\n"
-           "functions are equivalent to\n\n"
+           "value associated with @var{key} in @var{alist} is returned.  If @var{key} 
+is\n"
+           "not found return @var{default} (or @code{#f} if no @var{default} 
+argument\n"
+           "is supplied.  These functions are equivalent to\n\n"
            "@lisp\n"
            "(let ((ent (@var{associator} @var{key} @var{alist})))\n"
            "  (and ent (cdr ent)))\n"
@@ -219,13 +220,15 @@
     {
       return SCM_CDR (handle);
     }
-  return SCM_BOOL_F;
+  if (SCM_UNBNDP (dflt))
+    dflt = SCM_BOOL_F;
+  return dflt;
 }
 #undef FUNC_NAME
 
 
-SCM_DEFINE (scm_assv_ref, "assv-ref", 2, 0, 0,
-            (SCM alist, SCM key),
+SCM_DEFINE (scm_assv_ref, "assv-ref", 2, 1, 0,
+            (SCM alist, SCM key, SCM dflt),
            "Behaves like @code{assq-ref} but uses @code{eqv?} for key comparison.")
 #define FUNC_NAME s_scm_assv_ref
 {
@@ -236,13 +239,15 @@
     {
       return SCM_CDR (handle);
     }
-  return SCM_BOOL_F;
+  if (SCM_UNBNDP (dflt))
+    dflt = SCM_BOOL_F;
+  return dflt;
 }
 #undef FUNC_NAME
 
 
-SCM_DEFINE (scm_assoc_ref, "assoc-ref", 2, 0, 0,
-            (SCM alist, SCM key),
+SCM_DEFINE (scm_assoc_ref, "assoc-ref", 2, 1, 0,
+            (SCM alist, SCM key, SCM dflt),
            "Behaves like @code{assq-ref} but uses @code{equal?} for key comparison.")
 #define FUNC_NAME s_scm_assoc_ref
 {
@@ -253,7 +258,9 @@
     {
       return SCM_CDR (handle);
     }
-  return SCM_BOOL_F;
+  if (SCM_UNBNDP (dflt))
+    dflt = SCM_BOOL_F;
+  return dflt;
 }
 #undef FUNC_NAME
 

Reply via email to