This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

Preserve the case of cons cell data

7 posts · 6609 views

I have a variable of a list of cons cells here?
 (defparameter lookup-animal  '((Cat . kitten) (Bear . cub) (Cow . calf)))
Here is the function I use to parse through them
(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)
 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.
(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

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

This works:
(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)
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version). Temporary mirrors of aferomentioned: CLHS and a dark version.

Re: Preserve the case of cons cell data

Thanks for your reply, I really do appreciate it it: ) But, I found an even simpler answer I'd like to share with you.

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

Last edited by joeish80829 on , edited 1 time in total.

Re: Preserve the case of cons cell data

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

That name has very positive meaning,...Thanks for telling me...I just was curious?