help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: reading the C source of Emacs


From: Oliver Scholz
Subject: Re: reading the C source of Emacs
Date: Fri, 17 Jan 2003 05:55:40 +0100
User-agent: Gnus/5.090008 (Oort Gnus v0.08) Emacs/21.2 (i386-msvc-nt5.1.2600)

Thank you all for you advice. You helped to set me on the track. But
it is indeed a rather difficult read for me. My head dizzles is still
a bit dizzling.

Below are my very first steps in C: a version of `find-if' as a
starter. I'd appreciate any comment. (Did I miss something important,
for example?)

One thing that I noticed is that my `find-if' causes an infinite loop,
when applied to a recursive (?) list, à la:

(setq my-list (list 'alpha 'beta 'gamma))

(setcdr (cddr my-list) my-list)

(find-if (lambda (elt) (eq elt 'zeta)) my-list)

I couldn't think of a remedy for this. How could I solve this problem?
OTOH this happens to `copy-sequence', too. So maybe this is o.k.?

Index: src/fns.c
===================================================================
RCS file: /cvsroot/emacs/emacs/src/fns.c,v
retrieving revision 1.327
diff -u -r1.327 fns.c
--- src/fns.c   6 Jan 2003 15:41:17 -0000       1.327
+++ src/fns.c   17 Jan 2003 04:35:26 -0000
@@ -2913,6 +2913,57 @@
 
   return sequence;
 }
+
+#define FIND_IF_1(VARIABLE, PREDICATE, ELEMENT)    \
+       if (! NILP (call1 ((PREDICATE), (ELEMENT)))) \
+        {                                          \
+          (VARIABLE) = (ELEMENT);                  \
+          break;                                   \
+        }
+
+DEFUN ("find-if", Ffind_if, Sfind_if, 2, 2, 0,
+       doc: /* Return the first element for which PREDICATE returns non-nil. 
*/)
+     (predicate, sequence)
+     Lisp_Object predicate, sequence;
+{
+  Lisp_Object elt = Qnil;
+
+  if (STRINGP (sequence))
+    {
+      int nchars = SCHARS (sequence);
+      int cidx, bidx;
+
+      for (cidx = bidx = 0; cidx < nchars;)
+       {
+         int c;
+         FETCH_STRING_CHAR_ADVANCE (c, sequence, cidx, bidx);
+         FIND_IF_1 (elt, predicate, (make_number (c)));
+         QUIT;
+       }}
+  else if (VECTORP (sequence))
+    {
+      int i;
+      int len = ASIZE (sequence);
+
+      for (i = 0; i < len; i++)
+       FIND_IF_1 (elt, predicate, (AREF (sequence, i)));
+      QUIT;
+    }
+  else /* It ought to be a list. */
+    {
+      while (! NILP (sequence))
+       {
+         if (! (CONSP (sequence)))
+           return wrong_type_argument (Qsequencep, sequence);
+         else FIND_IF_1 (elt, predicate, (XCAR (sequence)));
+         sequence = XCDR (sequence);
+         QUIT;
+       }}
+  return elt;
+}
+
+    
+
 
 /* Anything that calls this function must protect from GC!  */
 
@@ -5580,6 +5631,8 @@
   defsubr (&Snconc);
   defsubr (&Smapcar);
   defsubr (&Smapc);
+  //  defsubr (&Sexplode);
+  defsubr (&Sfind_if);
   defsubr (&Smapconcat);
   defsubr (&Sy_or_n_p);
   defsubr (&Syes_or_no_p);
    Oliver
-- 
28 Nivôse an 211 de la Révolution
Liberté, Egalité, Fraternité!

reply via email to

[Prev in Thread] Current Thread [Next in Thread]