case of predicates



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

.



Relevant Pages

  • Re: Eval definition questions
    ... form) (cond-clauses form) ... because SICP just uses one long cond clause; ... cond-clauses (and may be cond-clause-test ... Our evaluator could ...
    (comp.lang.scheme)
  • Re: Sense lining--2nd try
    ... you have to break a clause, keep the subject and predicate together. ... I've never taken a speed reading course. ... I've worked on a lot of PowerPoint presentations, because I was required to do so for a job. ...
    (comp.fonts)
  • Re: Sublists question
    ... Since any of the variables can be s, this clause is certainly true. ... Here is an alternative predicate that does not have that problem. ... What Prolog are you using? ... Anyway, with the new sublist/2 predicate, the query, langford, ...
    (comp.lang.prolog)
  • Re: Search path program
    ... predicate is specified, EQL is used. ... the test-key is eql to any key for that clause", ... specifically mentioned the predicate. ... programmers would be designed like a well-written computer program. ...
    (comp.lang.lisp)
  • Re: knowledge base in FOL
    ... <- (let NewClause (st Clause Clause) ... (DEFUN tiny-prolog-loop NIL ... (COND ...
    (comp.lang.lisp)

Loading