newLISP Fan Club

Forum => newLISP in the real world => Topic started by: TedWalther on August 09, 2015, 02:10:12 PM

Title: case different from cond; doesn't allow "default"
Post by: TedWalther on August 09, 2015, 02:10:12 PM
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?
Title: Re: case different from cond; doesn't allow "default"
Post by: hartrock on August 09, 2015, 08:52:23 PM
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")))