From: ······@corporate-world.lisp.de
Subject: Re: 'case' special op for strings?
Date: 
Message-ID: <cf4b5e10-d9a8-41d1-ac53-acd091c83bbe@f20g2000yqg.googlegroups.com>
On 28 Nov., 23:45, "William James" <·········@yahoo.com> wrote:
> Thomas A. Russ wrote:
> > Carlo <······@carlocapocasa.com> writes:
>
> > > Is there any way to use the 'case' special op with other comparisons
> > > that 'eq', e.g. 'string='?
>
> > No.  You need to write your own macro for that.
>
> > I suspect that there are actually many such implementations around.
> > In any case, it isn't that hard to come up with your own macro:
>
> > (defmacro case* (item &rest clauses)
> >    (if (listp test)
> >        (destructuring-bind (object &key test) test
> >           (if test
> >                ...<build and return clauses here...
> >               `(case ,object ,@clauses)))
> >        `(case ,test ,@clauses)))
>
> > All that's left is to build the clauses.  You can do that by
> > introducing a GENSYM'd binding for the value of OBJECT and then
> > construct the clauses appropriately using either the TEST argument
> > directly or FIND/MEMBER with the appropriate :TEST argument.
>
> A macro isn't needed.
>
> Ruby:
>
> x = 5
> [
>   [2, 'two'],
>   [4, 'four'],
>   [6, 'six']
> ].find{|a,b| a > x }.first
>     ==>6

Wrong result. 'six' would have been more interesting.

Anyway, ruby isn't needed.

(find 5 '((2 two) (4 four) (6 six))
      :key 'first
      :test '<)
-> (6 SIX)