case of predicates
- From: "Jimka" <jimka@xxxxxxxxx>
- Date: 5 Nov 2006 04:57:19 -0800
Quite often a COND is simply a series of predicates all
applied to the same value, and the COND is essentially
finding the first predicate that is true of the original value.
Is there already a more primative way to do this in common lisp?
It is possible to write (as shown below) and not very pretty,
but perhaps there is some better way using built-in functions
with ?test and ?key keyword parameters?
I want to make a case statement where every clause of
the case simply tests a predicate applied to the same argument.
So it is sort of a case but with a ?test keyword (sort of!).
(defmacro case-predicate (obj &rest clauses)
(let ((var (gensym "var")))
(let ((expand-predicate (lambda (clause)
(if (eq t (car clause))
clause
`(( ,(car clause) ,var)
,@(cdr clause))))))
`(let ((,var ,obj))
(cond ,@(mapcar expand-predicate clauses))))))
(case-predicate expression
(null nil)
(numberp (format t "~A" expression))
(stringp expression)
(list-of-strings? (some-function-1 expression))
(list-of-numbers? (some-function-2 expression))
(t (error "did not match")))
.
- Follow-Ups:
- Re: case of predicates
- From: Pascal Costanza
- Re: case of predicates
- From: John Thingstad
- Re: case of predicates
- Prev by Date: Re: lisp implementations and scaling requirements
- Next by Date: Re: case of predicates
- Previous by thread: Re: Why is Lisp attacked on Reddit almost as often as Republicans?
- Next by thread: Re: case of predicates
- Index(es):
Relevant Pages
|
Loading