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

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

Re: Lisp: Functions for multiple comparisons


From: Pascal Bourguignon
Subject: Re: Lisp: Functions for multiple comparisons
Date: 21 Nov 2002 20:56:24 +0100
User-agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.2

"Stefan Monnier <foo@acm.com>" <monnier+gnu.emacs.help/news/@flint.cs.yale.edu> 
writes:

> >>>>> "Greg" == Greg Hill <ghill@synergymicro.com> writes:
> >     (meq (char-syntax (following-char)) ?w ?_)
> > instead of using
> >     (memq (char-syntax (following-char) '(?w ?_)))
> 
> memq is pretty fast given the general slowness of the elisp interpreter.
> If you really care about the few extra chars, then you can of course do
> 
>    (defun meq (x &rest xs) (memq x xs))
> 
> although I honestly don't see the point.
> 
> 
>         Stefan

Perhaps avoiding cutting too much:

Greg Hill <ghill@synergymicro.com> writes:

> I am aware of the built-in functions memq and member.  But what I
> really want is a pair of special forms that work more like 'or and
> 'and.  The first argument would be compared against all of the rest
> using either 'eq or 'equal, returning 't if any match was found.

So, Greg  wants a special form like  'or or 'and.  That  is, with lazy
evaluation of the remaining arguments!

(defmacro meq (key &rest values)
  `(let ((meq_key    ,key)
         (meq_values ',values))
    (while (and meq_values (not (eq meq_key (eval (car meq_values)))))
      (setq meq_values (cdr meq_values)))
    meq_values))

(show (macroexpand (quote (meq 12  (+ 9 1) (+ 10 2) (length (make-vector 1e9)) 
13))))

==> (let ((meq_key 12) (meq_values (quote ((+ 9 1) (+ 10 2) (length 
(make-vector 1000000000.0)) 13)))) (while (and meq_values (not (eq meq_key 
(eval (car meq_values))))) (setq meq_values (cdr meq_values))) meq_values)

(show (meq 12  (+ 9 1) (+ 10 2) (length (make-vector 1e9)) 13))

==> ((+ 10 2) (length (make-vector 1000000000.0)) 13)


But of course, it you only  use lists of constants like '(?w ?_), it's
better to use memq which is a primitive.
 
> For example, a special form using an 'eq comarison might be named 'meq
> and be called like:
> 
>       (meq (char-syntax (following-char)) ?w ?_)
> 
> instead of using
> 
>       (memq (char-syntax (following-char) '(?w ?_)))
> or
>       (or (eq (char-syntax (following-char)) ?w) (eq (char-syntax
> (following-char)) ?_))
> or
>       (let ((syntax (char-syntax (following-char)))) (or (eq syntax ?w)
> (eq syntax ?_)))
> 
> Is there already something like that that I simply am not yet aware
> of?  If not, am I wrong in thinking that the kind of special forms I
> am imagining would be computationally more efficient than any of the
> alternatives shown above?
> 
> --Greg


-- 
__Pascal_Bourguignon__                   http://www.informatimago.com/
----------------------------------------------------------------------
There is no worse tyranny than to force a man to pay for what he does not
want merely because you think it would be good for him.  -- Robert Heinlein


reply via email to

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