* complex data structure in elisp
@ 2009-08-25 14:22 Dirk80
2009-08-25 22:44 ` Bernardo
0 siblings, 1 reply; 5+ messages in thread
From: Dirk80 @ 2009-08-25 14:22 UTC (permalink / raw)
To: Help-gnu-emacs
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
--
View this message in context: http://www.nabble.com/complex-data-structure-in-elisp-tp25135295p25135295.html
Sent from the Emacs - Help mailing list archive at Nabble.com.
^ 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 ` 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: Joost Kremers @ 2009-08-25 14:45 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.
have you looked at elip?
<http://www.gnuvola.org/software/elip/>
may not do what you have in mind, though.
> 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.
with (require 'cl-macs) you have access to a defstruct.
but if you want to store a lot of such items, perhaps the emacs database is more
useful:
<http://www.gnuvola.org/software/edb/>
--
Joost Kremers joostkremers@yahoo.com
Selbst in die Unterwelt dringt durch Spalten Licht
EN:SiS(9)
^ 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 ` 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 ` 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
* Re: complex data structure in elisp
2009-08-25 14:22 complex data structure in elisp Dirk80
@ 2009-08-25 22:44 ` Bernardo
0 siblings, 0 replies; 5+ messages in thread
From: Bernardo @ 2009-08-25 22:44 UTC (permalink / raw)
To: Help-gnu-emacs
>
> sorry for this beginner question. But I'm very interested how you would
> represent the data structure of my example in elisp.
>
a few approaches you may consider are outlined in this thread
http://lists.gnu.org/archive/html/help-gnu-emacs/2009-02/msg00060.html
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2009-08-25 22:44 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-08-25 14:22 complex data structure in elisp Dirk80
2009-08-25 22:44 ` Bernardo
[not found] <mailman.5305.1251210181.2239.help-gnu-emacs@gnu.org>
2009-08-25 14:45 ` Joost Kremers
2009-08-25 15:06 ` Xah Lee
2009-08-25 16:01 ` Michael Ekstrand
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).