Page 1 of 1

Preserve the case of cons cell data

Posted: Thu Mar 20, 2014 7:47 pm
by joeish80829
I have a variable of a list of cons cells here?

Code: Select all

 (defparameter lookup-animal  '((Cat . kitten) (Bear . cub) (Cow . calf)))
Here is the function I use to parse through them

Code: Select all

(defun lookup-animal (name type)
  (if (eq type 'old)
    (setf name (car (assoc name lookup-animal)))
  (if (eq type 'young)
    (setf name (cdr (assoc name lookup-animal)))))
  name)

Code: Select all

 I run (lookup-animal 'Cat 'old) and it would output > CAT
 I run (lookup-animal 'Cat 'YOUNG) and it would output > KITTEN
The issue is I would like the name variable at the bottom of the list of defun lookup-animal to preserve the case of the animal list for example, Cat should print Cat not CAT, and Bear should print as Bear not BEAR. I looked over the internet for 3 hours and got nada...I got uppercase, lowercase no preserve case..

Here is another try ...It just prints "NAME" instead of the data...I'll still could use help figuring out how to preserve the case of the cons cell data.

Code: Select all

(defun lookup (name language o)
    (if (eq language 'lisp)
      (setf name (car (assoc name lookup)))
    (if (eq language 'C++)
      (setf name (cdr (assoc name lookup)))))

(setf (readtable-case *readtable*)  :preserve)
(SETF O (SYMBOL-NAME 'NAME))
(SETF (READTABLE-CASE *READTABLE*)  :UPCASE)
o)
.so if anyone can help me in the code above preserve the case of the output of the final variable name in the function lookup-animal while keeping it the red color of a PRINC not the pinkish color of FORMAT..I would really grateful..

Re: Preserve the case of cons cell data

Posted: Thu Mar 20, 2014 8:57 pm
by nuntius
What was the value of readtable-case when the symbols in the cons cells were read?

Case changes happen early in the reader algorithm. The string handed to INTERN has already had case modifications.

http://www.lispworks.com/documentation/ ... y/02_b.htm

If you don't want to modify the readtable (and face the resulting problems), you can protect individual symbols using vertical bars.
For example |Cat| will never be converted to CAT.

Re: Preserve the case of cons cell data

Posted: Fri Mar 21, 2014 4:32 am
by Goheeca
This works:

Code: Select all

(defun lookup-animal (name type)
  (if (eq type 'old)
    (setf name (car (assoc name lookup-animal)))
  (if (eq type 'young)
    (setf name (cdr (assoc name lookup-animal)))))
  name)

(setf (readtable-case *readtable*) :preserve)

(DEFPARAMETER LOOKUP-ANIMAL '((Cat . kitten) (Bear . cub) (Cow . calf)))

(LOOKUP-ANIMAL 'Cat 'OLD)

(LOOKUP-ANIMAL 'Cat 'YOUNG)

Re: Preserve the case of cons cell data

Posted: Sat Mar 22, 2014 1:53 pm
by joeish80829
Thanks for your reply, I really do appreciate it it: ) But, I found an even simpler answer I'd like to share with you.

Code: Select all

LISP-CV> (defparameter a '((|Bear| . |bear|) (|beaR| . |BEAR|)))
A
LISP-CV> a
((|Bear| . |bear|) (|beaR| . BEAR))

Re: Preserve the case of cons cell data

Posted: Sat Mar 22, 2014 1:59 pm
by joeish80829
Thanks nuntius...I just noticed your reply and that is a perfect answer...you'd be suprised how many people don't know that one...What does nuntius mean anyway...just curious....Thanks again: )

Re: Preserve the case of cons cell data

Posted: Sat Mar 22, 2014 5:02 pm
by nuntius
nuntius: Lingua Romana, http://en.wiktionary.org/wiki/nuntius

Please don't shoot me. ;)

Re: Preserve the case of cons cell data

Posted: Tue Mar 25, 2014 1:20 pm
by joeish80829
That name has very positive meaning,...Thanks for telling me...I just was curious?