Primitive "case" does not work

Q&A's, tips, howto's
Post Reply
lyl
Posts: 37
Joined: Sun Mar 25, 2018 5:00 am

Primitive "case" does not work

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

ralph.ronnquist
Posts: 216
Joined: Mon Jun 02, 2014 1:40 am
Location: Melbourne, Australia

Re: Primitive "case" does not work

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

lyl
Posts: 37
Joined: Sun Mar 25, 2018 5:00 am

Re: Primitive "case" does not work

Post 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?

ralph.ronnquist
Posts: 216
Joined: Mon Jun 02, 2014 1:40 am
Location: Melbourne, Australia

Re: Primitive "case" does not work

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

Post Reply