help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: sharing list structure


From: rgb
Subject: Re: sharing list structure
Date: 24 Mar 2005 21:44:42 -0800
User-agent: G2/0.2

> The data structure that I think ought to be capable of handling all
of
> this would be a list B that contains
>
>  ((title1, H1, F1), (title2, H2, F2), ..., (titlen, Hn, Fn))
>

Well I'm not sure now if you're re-explaining the problem for my
benefit or showing me you've found the answer.  Because the above
(with commas removed) seems to be a perfectly useable answer.


(setq all-my-lists
  '((key1 (h1a h1b h1c ...) (f1a f1b f1c ...))
    (key2 (h2a h2b h2c ...) (f2a f2b f2c ...))
     ...))

It should't be too hard to write some functions that mannage a
set of nested lists such as that in whatever way is convenient to
your application.

For example:

(setq all '((a (h1 h2 h3)(f1 f2 f3))       ;test data
            (b (ha hb hc)(fa fb fc))))

(defun get^h (key index)
  "get h[index] value within KEY list"
  (nth index (cadr (assoc key all))))

(get^h 'b 2)
hc

(defun delete^h (key index)
  "remove h[index] value from KEY list."
  (if (< 0 index)
      ;; the car of the list is not affected so just change the list
      ;; I used (nth 1 ...) rather than (cadr ...) so it's more obvious
      ;; how this could have a 3rd argument and not just access h.
      (setcdr (nthcdr (1- index) (nth 1 (assoc key all)))
              (nthcdr (1+ index) (nth 1 (assoc key all))))
    ;; when the car changes the outer list holding it must change
    (setcar (cdr (assoc key all))
            (nthcdr 1 (cadr (assoc key all))))))

(get^h 'a 1)
h2
(delete^h 'a 1)
(get^h 'a 1)
h3

(defun insert^h (key index value)
  "insert a new h value at h[index] value from KEY list."
  (if (< 0 index)
      (setcdr (nthcdr (1- index) (nth 1 (assoc key all)))
              (cons value (nthcdr index (nth 1 (assoc key all)))))
    (setcar (nthcdr 1 (assoc key all))
            (cons value (nthcdr index (nth 1 (assoc key all)))))))

(insert^h 'a 1 'h4)
(get^h 'a 1)
h4
(get^h 'a 2)
h3

Replace is more of the same...



reply via email to

[Prev in Thread] Current Thread [Next in Thread]