From: ····@manetheren.stcloudstate.edu
Subject: Alist question: how to use value instead of symbol?
Date: 
Message-ID: <87r9yyxtz3.fsf@manetheren.stcloudstate.edu>
I'm trying to write a small set of utilities to make editing code for
large projects a little easier (at least I think so..), but I've run
into a bit of a snag.  The function that is giving me the trouble is
one that is supposed to tag a new association onto an alist (so I can
make use of the completing-read function).  The association links a
class name with a register.  I'm using a simple iterator for the
register, and the name of the class is to be supplied when the
function is called.  The problem is, I can't seem to find a way to use
the values of the variables as the elements of the association,
instead of the symbols themselves.

-- This one give me a large list of (CLASS . proj-register-i) items to
-- the alist.

(defun proj-cons-class-alist (CLASS)
  "Adds a class entry onto the proj-class-alist"
  (setq proj-class-alist (cons '(CLASS . proj-register-i) proj-class-alist))
  (setq proj-register-i (+ 1 proj-register-i)))

-- With this one, I wanted to see if I could change the values in the
-- alist..the problem was every call to this changed all elements of
-- the list instead of just the first one, so I would get a large list
-- of (Tidbit . 7) elements (for example).

(defun proj-cons-class-alist (CLASS)
  "Adds a class entry onto the proj-class-alist"
  (setq proj-class-alist (cons '(TEMP . INT) proj-class-alist))
  (setcar (car proj-class-alist) CLASS)
  (setcdr (car proj-class-alist) proj-register-i)
  (setq proj-register-i (+ 1 proj-register-i)))

Does anyone know what I'm doing wrong with these functions, how I can
refer to the values of the variables instead of the symbol names, or if
there is a better way to go about this?

From: Kai Grossjohann
Subject: Re: Alist question: how to use value instead of symbol?
Date: 
Message-ID: <vafzpdmcmlk.fsf@ramses.cs.uni-dortmund.de>
>>>>> ····@manetheren.stcloudstate.edu writes:

  >   (setq proj-class-alist (cons '(CLASS . proj-register-i) proj-class-alist))

Try these:

,-----
| (setq a 1)         
| (setq b 2)         
| (setq x '(a . b))  
| (setq y (cons a b))
`-----

See?

Btw, to add something to a list if it isn't there yet, try
add-to-list: (add-to-list 'proj-class-alist (cons foo bar))
There's also the Common Lisp function push.

kai
-- 
Abort this operation?   [Abort]  [Cancel]
From: Michael Welsh Duggan
Subject: Re: Alist question: how to use value instead of symbol?
Date: 
Message-ID: <v1tvhoadr9f.fsf@peoria.mt.cs.cmu.edu>
····@manetheren.stcloudstate.edu writes:

> I'm trying to write a small set of utilities to make editing code for
> large projects a little easier (at least I think so..), but I've run
> into a bit of a snag.  The function that is giving me the trouble is
> one that is supposed to tag a new association onto an alist (so I can
> make use of the completing-read function).  The association links a
> class name with a register.  I'm using a simple iterator for the
> register, and the name of the class is to be supplied when the
> function is called.  The problem is, I can't seem to find a way to use
> the values of the variables as the elements of the association,
> instead of the symbols themselves.
> 
> -- This one give me a large list of (CLASS . proj-register-i) items to
> -- the alist.
> 
> (defun proj-cons-class-alist (CLASS)
>   "Adds a class entry onto the proj-class-alist"
>   (setq proj-class-alist (cons '(CLASS . proj-register-i) proj-class-alist))
>   (setq proj-register-i (+ 1 proj-register-i)))

Well, with this function, you have a (quote (CLASS . proj-register-i))
as your first argument.  The `quote' tells LISP not to evaluate its
argument.  Off hand, one easy way to do this is 

(setq proj-class-alist (cons (cons CLASS proj-register-i)
                             proj-class-alist))

Another, more esoteric way of doing this is 

(require 'backquote)
(setq proj-class-alist (const `(,CLASS . ,proj-register-i)
                              proj-class-alist))

Note specifically the use of the backquote macro.  The way I would
write the statement personally, is:

(require 'cl)
(push (cons CLASS proj-class-alist) proj-class-alist)

`C-h f' any functions you aren't familiar with.

> -- With this one, I wanted to see if I could change the values in the
> -- alist..the problem was every call to this changed all elements of
> -- the list instead of just the first one, so I would get a large list
> -- of (Tidbit . 7) elements (for example).
> 
> (defun proj-cons-class-alist (CLASS)
>   "Adds a class entry onto the proj-class-alist"
>   (setq proj-class-alist (cons '(TEMP . INT) proj-class-alist))
>   (setcar (car proj-class-alist) CLASS)
>   (setcdr (car proj-class-alist) proj-register-i)
>   (setq proj-register-i (+ 1 proj-register-i)))

Off hand, just by looking at it, I couldn't say why you were getting
that behaviour with this function.  I get the same behaviour, however,
so I think it probably has to do with the way proj-cons-class-alist is
compiled.  I.e., there is a static '(TEMP . INT) hanging around, which
you keep on changing the value of.  Since every element of the alist
points to this static member, all the elements are the same.

-- 
Michael Duggan
(····@cs.cmu.edu)