[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: How to describe something in Lisp?
From: |
Thierry Volpiatto |
Subject: |
Re: How to describe something in Lisp? |
Date: |
Tue, 03 Feb 2009 17:24:21 +0100 |
User-agent: |
Gnus/5.110011 (No Gnus v0.11) Emacs/23.0.90 (gnu/linux) |
Hi,
Johan Andersson <johan.rejeep@gmail.com> writes:
> Hi!
>
> As a Java and Ruby programmer I sometimes find it hard to code Lisp. Right
> now I'm working on a minor mode for which the structure would obvious for me
> in Java or Ruby, but in Lisp is a riddle.
>
> I will not describe the mode itself, but give a description of the problem.
> Say I want to store a list of people in a file. And for each person, also
> some information on them in the format:
> name|age|married|sex
>
> Each time I start the mode, that file should be parsed in to some
> datastructure (which kind of is the problem). And on save, the file would be
> updated. For me it's obvious to represent a person with a class:
> class Person
> var name, age, married, sex
>
> methods...
> end
>
> Then I could easy update attributes on the objects, remove and add people
> and then update the file.
>
> I tried with a couple of solutions for this in Lisp:
>
> 1) One list named people:
> (
> (name age married sex)
> ...
> )
>
> 2) Multiple lists named name, age, married and sex where the index decides
> the connection between the lists:
> (name1 name2)
> (age1 age2)
> (married1 married2)
> (sex1 sex2)
>
> 3) Same as two, but with arrays:
> [name1 name2]
> [age1 age2]
> [married1 married2]
> [sex1 sex2]
>
>
> Each way above has their disadvantages and I think none of them is good
> enough. I read something about object orientation in lisp, but I have never
> seen this be used in Emacs. So my question is basically: What is the best
> way to model something in lisp, that you in an object oriented language
> would model with a class.
>
> Thanks!
Does that help?
,----
| ELISP> (defstruct People name age)
| People
| ELISP> (defvar P (make-People :name "thierry" :age "45"))
| P
| ELISP> (People-name P)
| "thierry"
| ELISP> (People-age P)
| "45"
| ELISP> (setf (People-age P) "46")
| "46"
| ELISP> (People-age P)
| "46"
`----
--
A + Thierry Volpiatto
Location: Saint-Cyr-Sur-Mer - France
Message not available