Page 1 of 1

Primitive "case" does not work

Posted: Wed Jun 05, 2019 8:56 am
by lyl
I construct the following function by the primitive "case"

Code: Select all

(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"?

Re: Primitive "case" does not work

Posted: Wed Jun 05, 2019 9:37 am
by ralph.ronnquist
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

Code: Select all

(define (f obj a da b db)
  (letex ((a a) (b b))
      (case obj
          (a da)
          (b db)
          (true obj)
   )))

Re: Primitive "case" does not work

Posted: Wed Jun 05, 2019 9:48 am
by lyl
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?

Re: Primitive "case" does not work

Posted: Wed Jun 05, 2019 12:43 pm
by ralph.ronnquist
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.