From: Oliver Scholz <alkibiades@gmx.de>
Subject: Re: reading the C source of Emacs
Date: Fri, 17 Jan 2003 05:55:40 +0100 [thread overview]
Message-ID: <uznq0pe7n.fsf@ID-87814.user.dfncis.de> (raw)
In-Reply-To: 5l3cnydqys.fsf@rum.cs.yale.edu
[-- Attachment #1: Type: text/plain, Size: 685 bytes --]
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.?
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: fns.diff --]
[-- Type: text/x-patch, Size: 1860 bytes --]
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;
+}
+
+
+
\f
/* 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);
[-- Attachment #3: Type: text/plain, Size: 85 bytes --]
Oliver
--
28 Nivôse an 211 de la Révolution
Liberté, Egalité, Fraternité!
[-- Attachment #4: Type: text/plain, Size: 151 bytes --]
_______________________________________________
Help-gnu-emacs mailing list
Help-gnu-emacs@gnu.org
http://mail.gnu.org/mailman/listinfo/help-gnu-emacs
next prev parent reply other threads:[~2003-01-17 4:55 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2003-01-11 17:03 reading the C source of Emacs Oliver Scholz
2003-01-11 17:43 ` David Kastrup
2003-01-12 16:31 ` Oliver Scholz
2003-01-11 21:26 ` Kai Großjohann
2003-01-12 16:37 ` Oliver Scholz
2003-01-12 19:56 ` Kai Großjohann
2003-01-12 21:45 ` Oliver Scholz
2003-01-12 22:16 ` David Kastrup
2003-01-12 20:59 ` Stefan Monnier <foo@acm.com>
2003-01-17 4:55 ` Oliver Scholz [this message]
2003-01-17 11:41 ` David Kastrup
2003-01-17 17:09 ` Kai Großjohann
2003-01-13 5:52 ` Janusz S. Bień
[not found] ` <mailman.202.1042437305.21513.help-gnu-emacs@gnu.org>
2003-01-13 6:17 ` Miles Bader
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=uznq0pe7n.fsf@ID-87814.user.dfncis.de \
--to=alkibiades@gmx.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).