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

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

Re: complex data structure in elisp


From: Michael Ekstrand
Subject: Re: complex data structure in elisp
Date: Tue, 25 Aug 2009 11:01:34 -0500
User-agent: Mozilla-Thunderbird 2.0.0.19 (X11/20090103)

Dirk80 wrote:
> Hi,
> 
> sorry for this beginner question. But I'm very interested how you would
> represent the data structure of my example in elisp.
> 
> Here my example:
> I want to implement a vocabulary trainer in elisp.
> 
> I have units. A unit is consisting of lessons and lessons are consistng of
> sublessons. One sublesson is consisting of vocabularies. A vocabulary is
> consisting of an audio-file, picture file and a text.
> 
> Here how I would do it in C:
> 
> <c structure definitions snipped>

For your list structures, just use Lisp lists (or, if you need random
access, arrays).  You can learn more about them in the Emacs Lisp intro
and reference manual.

For your other structures, such as Vocabulary, I'd recommend defstruct.
 It's a macro from Common Lisp, made available in Emacs Lisp via the
`cl' package, which allows you to define structures with accessors, etc.
 For example, your Vocabulary struct:

(defstruct vocabulary audio-file picture-file text)

You can then do:

;; Make a new vocabulary entry
(make-vocabulary :audio-file "snd.au" :picture-file "pic.jpg"
     :text "Hi!")
;; Retrieve the vocab text from vocab object vobj
(vocabulary-text vobj)
;; Check if x is a vocabulary object
(vocabulary-p x)
;; Set the audio file
(setf (vocabulary-audio-file vobj) "othersnd.au")

Look in the CL info document for more on structures.

You could also use eieio, an Emacs object system similar to the Common
Lisp Object System, but it's more complex and isn't included with Emacs
by default, so I wouldn't suggest using it unless you need its
additional functionality.

- Michael


reply via email to

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