From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Xah Lee Newsgroups: gmane.emacs.help Subject: Re: complex data structure in elisp Date: Tue, 25 Aug 2009 08:06:52 -0700 (PDT) Organization: http://groups.google.com Message-ID: <73e9425f-4d38-49e1-8777-3c7b26280f8a@2g2000prl.googlegroups.com> References: NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Trace: ger.gmane.org 1251218595 25831 80.91.229.12 (25 Aug 2009 16:43:15 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 25 Aug 2009 16:43:15 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Tue Aug 25 18:43:08 2009 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1Mfz6u-00053A-8F for geh-help-gnu-emacs@m.gmane.org; Tue, 25 Aug 2009 18:43:04 +0200 Original-Received: from localhost ([127.0.0.1]:58047 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Mfz6t-0002zI-Oq for geh-help-gnu-emacs@m.gmane.org; Tue, 25 Aug 2009 12:43:03 -0400 Original-Path: news.stanford.edu!newsfeed.stanford.edu!postnews.google.com!2g2000prl.googlegroups.com!not-for-mail Original-Newsgroups: gnu.emacs.help,comp.emacs Original-Lines: 128 Original-NNTP-Posting-Host: 76.102.12.87 Original-X-Trace: posting.google.com 1251212812 28470 127.0.0.1 (25 Aug 2009 15:06:52 GMT) Original-X-Complaints-To: groups-abuse@google.com Original-NNTP-Posting-Date: Tue, 25 Aug 2009 15:06:52 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: 2g2000prl.googlegroups.com; posting-host=76.102.12.87; posting-account=bRPKjQoAAACxZsR8_VPXCX27T2YcsyMA User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US) AppleWebKit/530.5 (KHTML, like Gecko) Chrome/2.0.172.39 Safari/530.5, gzip(gfe), gzip(gfe) Original-Xref: news.stanford.edu gnu.emacs.help:172370 comp.emacs:98651 X-Mailman-Approved-At: Tue, 25 Aug 2009 12:36:22 -0400 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:67517 Archived-At: 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 o= f > 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 =3D > "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. =E2=80=9C[3 "a", 8, 7, "something"]=E2=80=9D, an= d it is fast to access random elements, but very slow to add or delete elements. Lists are written as e.g. =E2=80=9C(list 3 7 9 "some")=E2=80=9D or =E2=80= =9C'(3 7 9 "some")=E2=80=9D, 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: =E2=80=A2 Emacs Lisp Basics http://xahlee.org/emacs/elisp_basics.html =E2=80=A2 Elisp Lesson: Hash Table http://xahlee.org/emacs/elisp_hash_table.html =E2=80=A2 Sequences Arrays Vectors - GNU Emacs Lisp Reference Manual http://xahlee.org/elisp/Sequences-Arrays-Vectors.html =E2=80=A2 Lists - GNU Emacs Lisp Reference Manual http://xahlee.org/elisp/Lists.html Here's a criticism of lisp's list problem. =E2=80=A2 Fundamental Problems of Lisp http://xahlee.org/UnixResource_dir/writ/lisp_problems.html Xah =E2=88=91 http://xahlee.org/ =E2=98=84