Preserve the case of cons cell data

Discussion of Common Lisp
Post Reply
joeish80829
Posts: 153
Joined: Tue Sep 03, 2013 5:32 am

Preserve the case of cons cell data

Post by joeish80829 » Thu Mar 20, 2014 7:47 pm

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..

nuntius
Posts: 538
Joined: Sat Aug 09, 2008 10:44 am
Location: Newton, MA

Re: Preserve the case of cons cell data

Post by nuntius » Thu Mar 20, 2014 8:57 pm

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.

Goheeca
Posts: 271
Joined: Thu May 10, 2012 12:54 pm
Contact:

Re: Preserve the case of cons cell data

Post by Goheeca » Fri Mar 21, 2014 4:32 am

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)
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.

joeish80829
Posts: 153
Joined: Tue Sep 03, 2013 5:32 am

Re: Preserve the case of cons cell data

Post by joeish80829 » Sat Mar 22, 2014 1:53 pm

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))
Last edited by joeish80829 on Sat Mar 22, 2014 2:00 pm, edited 1 time in total.

joeish80829
Posts: 153
Joined: Tue Sep 03, 2013 5:32 am

Re: Preserve the case of cons cell data

Post by joeish80829 » Sat Mar 22, 2014 1:59 pm

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

nuntius
Posts: 538
Joined: Sat Aug 09, 2008 10:44 am
Location: Newton, MA

Re: Preserve the case of cons cell data

Post by nuntius » Sat Mar 22, 2014 5:02 pm

nuntius: Lingua Romana, http://en.wiktionary.org/wiki/nuntius

Please don't shoot me. ;)

joeish80829
Posts: 153
Joined: Tue Sep 03, 2013 5:32 am

Re: Preserve the case of cons cell data

Post by joeish80829 » Tue Mar 25, 2014 1:20 pm

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

Post Reply