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
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
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"
--
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.
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
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
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.