2009-08-25

emacs lisp data structure

On Aug 25, 7:22 am, 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:
>
> 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/

No comments:

Post a Comment