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

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

Re: How to describe something in Lisp?


From: Tassilo Horn
Subject: Re: How to describe something in Lisp?
Date: Tue, 03 Feb 2009 17:46:57 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.0.90 (gnu/linux)

Johan Andersson <johan.rejeep@gmail.com> writes:

Hi Johan,

> Then I could easy update attributes on the objects, remove and add
> people and then update the file.
>
> I tried with a couple of solutions for this in Lisp:
>
> 1) One list named people:
> (
>   (name age married sex)
>   ...
> )

I think a list of plists would be quite intuitive to someone with an OO
background.  Here's a quick and dirty snippet which should get you
started:

--8<---------------cut here---------------start------------->8---
(defvar persons
  '((:name "Klaus" :age 36 :sex male)
    (:name "Claudia" :age 31 :sex female)))

(defun print-persons (buffer)
  (set-buffer buffer)
  (dolist (person persons)
    (insert (format "\n;; %s, %s, %s"
                    (plist-get person :name)
                    (plist-get person :age)
                    (plist-get person :sex)))))

(print-persons (current-buffer)) ;; <== C-x C-e here!
;; Klaus, 36, male
;; Claudia, 31, female

(defun set-property (name prop newval)
  (let ((list persons))
    (while (not (string= (plist-get (car list) :name) name))
      (setq list (cdr list)))
    (when (not (null list))
      (let ((plist (car list)))
        (setq persons (remove plist persons))
        (setq persons (cons (plist-put plist prop newval) persons))))))

;; Adjust the ages
(set-property "Klaus" :age 37)
(set-property "Claudia" :age 32)

(print-persons (current-buffer)) ;; <== C-x C-e here!
;; Claudia, 32, female
;; Klaus, 37, male
--8<---------------cut here---------------end--------------->8---

I'm assume that the name (the :name property) is unique, here.

Bye,
Tassilo





reply via email to

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