all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Michael Ekstrand <michael@elehack.net>
To: help-gnu-emacs@gnu.org
Subject: Re: complex data structure in elisp
Date: Tue, 25 Aug 2009 11:01:34 -0500	[thread overview]
Message-ID: <h711rs$g72$1@aioe.org> (raw)
In-Reply-To: mailman.5305.1251210181.2239.help-gnu-emacs@gnu.org

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


  parent reply	other threads:[~2009-08-25 16:01 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <mailman.5305.1251210181.2239.help-gnu-emacs@gnu.org>
2009-08-25 14:45 ` complex data structure in elisp Joost Kremers
2009-08-25 15:06 ` Xah Lee
2009-08-25 16:01 ` Michael Ekstrand [this message]
2009-08-25 14:22 Dirk80
2009-08-25 22:44 ` Bernardo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='h711rs$g72$1@aioe.org' \
    --to=michael@elehack.net \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.