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

Q&A's, tips, howto's

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

Postby TedWalther » Sun Aug 09, 2015 9:10 pm

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.
TedWalther
 
Posts: 605
Joined: Mon Feb 05, 2007 1:04 am
Location: Abbotsford, BC

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

Postby hartrock » Mon Aug 10, 2015 3:52 am

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")))
hartrock
 
Posts: 136
Joined: Wed Aug 07, 2013 9:37 pm


Return to newLISP in the real world

Who is online

Users browsing this forum: No registered users and 2 guests

cron