* Re: complex data structure in elisp
[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
2 siblings, 0 replies; 5+ messages in thread
From: Xah Lee @ 2009-08-25 15:06 UTC (permalink / raw)
To: help-gnu-emacs
On Aug 25, 7:22 am, Dirk80 <d...@dirkundsari.de> 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:
>
> struct Vocabulary
> {
> char audio_file[255];
> char picture_file[255];
> char text[1000];
>
> };
>
> struct SubLesson
> {
> int nb_vocabularies;
> struct Vocabulary[1000];
>
> };
>
> struct Lesson
> {
> int nb_sub_lessons;
> struct SubLesson sub_lessons[10];
>
> };
>
> struct Unit
> {
> int nb_lessons;
> struct Lesson lessons[10];
>
> };
>
> struct UnitList
> {
> int nb_units;
> struct Unit units[8];
>
> };
>
> e.g. Unit 4, Lesson 7, Sublesson 2, Vocabulary 1, audio-file
> struct UnitList unit_list;
> unit_list.units[3].lessons[6].sub_lessons[1].vocabulary[0].audio_file =
> "hello.wav";
>
> Now to the details of using. Because I think this is important in elisp
> because of performance.
> This "UnitList" shall be initialised with content. During runtime it will
> never be changed.
>
> Thank you a lot in advance for your help
> Dirk
emacs lisp, pretty much like other scripting lang, has variously
called arrays, lists, hash tables, ... etc., and the types be nested
rather flexibly.
Specifically, emacs lisp has it its terminology: arrays, lists,
associative lists, and hash tables. Their difference is primarily
their algorithmic acess properties.
Arrays are written as e.g. “[3 "a", 8, 7, "something"]”, and it is
fast to access random elements, but very slow to add or delete
elements.
Lists are written as e.g. “(list 3 7 9 "some")” or “'(3 7 9 "some")”,
underneath they are cons cells like this: (cons 3 (cons 7 (cons 9
(cons "some" nil)))).
Lists are fast to prepend, but slow if you need random access to
elements in middle.
Associative lists are pretty much a list of cons pairs. Extra
difference is that you have easy interface with key and value
structure. You can query a data, query a value, get all keys, get all
values etc.
Hash is interface wise just list of pairs, but implemnted in a
different way (not as lists or cons), so that it's extremely fast to
query any element. Typically used for largish pairs. (say, thousands)
There is no syntax to build hash directly. You have to build them
element by element. (this is a common complaint and a problem of lisp)
So, if you want your data structure in memory, you pretty much use a
nested mix of one of the above. Exactly how you mix and nest them
depends on your app. I suppose if you have data like audio files or
large text, your values in your structure will just point to them.
e.g. as file paths.
i noticed few years ago there are already a couple or more flash card
or dictionary type of apps written in elisp. Might look around.
here's lisp basics:
• Emacs Lisp Basics
http://xahlee.org/emacs/elisp_basics.html
• Elisp Lesson: Hash Table
http://xahlee.org/emacs/elisp_hash_table.html
• Sequences Arrays Vectors - GNU Emacs Lisp Reference Manual
http://xahlee.org/elisp/Sequences-Arrays-Vectors.html
• Lists - GNU Emacs Lisp Reference Manual
http://xahlee.org/elisp/Lists.html
Here's a criticism of lisp's list problem.
• Fundamental Problems of Lisp
http://xahlee.org/UnixResource_dir/writ/lisp_problems.html
Xah
∑ http://xahlee.org/
☄
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: complex data structure in elisp
[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
2 siblings, 0 replies; 5+ messages in thread
From: Michael Ekstrand @ 2009-08-25 16:01 UTC (permalink / raw)
To: help-gnu-emacs
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
^ permalink raw reply [flat|nested] 5+ messages in thread