From: Carlo
Subject: 'case' special op for strings?
Date: 
Message-ID: <947b9e1b-1139-4616-a4a4-7330d4e95f8e@s9g2000prm.googlegroups.com>
Is there any way to use the 'case' special op with other comparisons
that 'eq', e.g. 'string='?

Thanks for your help!

Carlo

From: Thomas A. Russ
Subject: Re: 'case' special op for strings?
Date: 
Message-ID: <ymiskpnplca.fsf@blackcat.isi.edu>
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.

-- 
Thomas A. Russ,  USC/Information Sciences Institute
From: William James
Subject: Re: 'case' special op for strings?
Date: 
Message-ID: <ggpsap02hvi@enews1.newsguy.com>
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
From: William James
Subject: Re: 'case' special op for strings?
Date: 
Message-ID: <ggpsj101f8d@enews2.newsguy.com>
William James 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

x = 5
[
  [2, 'two'],
  [4, 'four'],
  [6, 'six']
].find{|a,b| a > x }.last
    ==>"six"

-- 
From: Volkan YAZICI
Subject: Re: 'case' special op for strings?
Date: 
Message-ID: <06c5dd25-a525-4b5d-8e4c-3a375739610e@l42g2000yqe.googlegroups.com>
On Nov 19, 6:55 pm, Carlo <······@carlocapocasa.com> wrote:
> Is there any way to use the 'case' special op with other comparisons
> that 'eq', e.g. 'string='?

See EXTENDED-CASE at http://paste.lisp.org/display/63037 address.


Regards.
From: Giovanni Gigante
Subject: Re: 'case' special op for strings?
Date: 
Message-ID: <49245cac$0$41660$4fafbaef@reader4.news.tin.it>
Carlo wrote:
> Is there any way to use the 'case' special op with other comparisons
> that 'eq', e.g. 'string='?


discussed here:
http://groups.google.it/group/comp.lang.lisp/browse_thread/thread/69319dd518f57515
From: Giovanni Gigante
Subject: Re: 'case' special op for strings?
Date: 
Message-ID: <49246047$0$41654$4fafbaef@reader4.news.tin.it>
Giovanni Gigante wrote:
> Carlo wrote:
>> Is there any way to use the 'case' special op with other comparisons
>> that 'eq', e.g. 'string='?


There is also an apache-licensed implementation of "CASE-USING" (which 
is what you are asking for) here:
http://gbbopen.org/svn/GBBopen/trunk/source/tools/tools.lisp
and its description here:
http://gbbopen.org/hyperdoc/ref-case-using.html
From: sross
Subject: Re: 'case' special op for strings?
Date: 
Message-ID: <0d748ae9-e1f5-4eef-a8ba-074fa4c1c32f@q9g2000yqc.googlegroups.com>
On Nov 19, 4:55 pm, Carlo <······@carlocapocasa.com> wrote:
> Is there any way to use the 'case' special op with other comparisons
> that 'eq', e.g. 'string='?
>
> Thanks for your help!
>
> Carlo

There is also some discussion of this here
http://groups.google.com/group/comp.lang.lisp/browse_frm/thread/562813a92d510073/a79ba3b04895a4d8?lnk=gst

Erik Naggum's solution is particularly instructive.