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

Q&A's, tips, howto's
Locked
TedWalther
Posts: 608
Joined: Mon Feb 05, 2007 1:04 am
Location: Abbotsford, BC
Contact:

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

Post by TedWalther »

Lately, when I've been using cond, I make use of a trick for cond:
(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!
(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
Posts: 136
Joined: Wed Aug 07, 2013 9:37 pm

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

Post by hartrock »

This works (from the manual):

Code: Select all

(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):

Code: Select all

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

Locked