case different from cond; doesn't allow "default"

Started by TedWalther, August 09, 2015, 02:10:12 PM

Previous topic - Next topic

TedWalther

Lately, when I've been using cond, I make use of a trick for cond:


Quote
(cond

  ((= a b) foo)

  (default bar))


This works, because "default" is a builtin function with an address, so it always evaluates to true.  I was actually shocked that there is a builtin function named "default".  Had to look it up to see what it did.



So, I thought, I will try this trick also for a case statement!


Quote
(case a

  (b foo)

  (default bar))


This doesn't work.  But (true bar) instead of (default bar) does work.  Lutz, if "true" is a default value for case statement, can we have "default" work as well?
Cavemen in bearskins invaded the ivory towers of Artificial Intelligence.  Nine months later, they left with a baby named newLISP.  The women of the ivory towers wept and wailed.  \"Abomination!\" they cried.

hartrock

#1
This works (from the manual):
(define (translate n)
  (case n
    (1 "one")
    (2 "two")          
    (3 "three")
    (4 "four")
    (true "Can't translate this")))

or this (good to use for error messages, if an unexpected condition occurs):

(define (translate n)
  (cond
   ((= n 1) "one")
   ((= n 2) "two")          
   ((= n 3) "three")
   ((= n 4) "four")
   ("default" "Can't translate this")))