Primitive "case" does not work

Started by lyl, June 05, 2019, 01:56:05 AM

Previous topic - Next topic

lyl

I construct the following function by the primitive "case"
(define (f obj a da b db)
  (case obj
(a da)
(b db)
(true obj)
))
(f 1 1 "a" 2 "b") ;;=> I get 1, but what I want is "a".


What is the cause? And how to get what I want in the above code by the use of "case"?

rrq

#1
Yes, as you know, the case term does not evaluate the branch keys, so you'll have to resort to a letex embedding, as in
(define (f obj a da b db)
  (letex ((a a) (b b))
      (case obj
          (a da)
          (b db)
          (true obj)
   )))

lyl

#2
Many thanks @ ralph.ronnquist for your solution!!

Though I can't understand why the case term is designed not to evaluate the branch keys. Anyone knows the reason?

rrq

#3
I suppose it links back to how the case term works in other Lisp variations.



There are also other conditional term forms such as if and cond to fill the need. Perhaps the prior question would be to ponder why having a case term at all.