Веб Зображення Новини Групи Блоги Перекладач Gmail Ще »
Групи, які ви переглядали нещодавно | Довідка | Увійти
Головна сторінка Груп Google
Is there a better way to store this?
Занадто багато тем, що мають бути показані першими. Для того, щоб показати тему першою, зніміть цю опцію з іншої теми.
Під час обробки вашого запиту сталася помилка. Будь ласка, повторіть вашу спробу пізніше.
флаг
  14 повідомлення - Згорнути всі  -  Перекласти все вказаною мовою: Перекладено (переглянути всі оригінали)
Група, до якої ви додаєте допис, - група Usenet. Відтак, будь-хто в Інтернеті бачитиме вашу електронну адресу.
Вашу відповідь не було надіслано.
Ваш допис надіслано
 
Від:
Кому:
Копія:
Продолжить:
Додати копію: | Додати продовження: | Редагувати тему
Тема:
Підтвердження:
З метою підтвердження введіть символи, наведені на зображенні нижче, або числа, які чуєте, натиснувши значок доступу. Прослухайте і введіть цифри, що чуєте
 
John Thingstad  
Переглянути профіль   Перекласти вказаною мовою: Перекладено (переглянути оригінал)
 Більше налаштувань 6 Лис, 18:08
Групи новин: comp.lang.lisp
Від: John Thingstad <jpth...@online.no>
Дата: Fri, 06 Nov 2009 10:08:46 -0600
Місцевий час: Пт 6 Лис 2009 18:08
Тема: Is there a better way to store this?
I am  writing a 'guess the animal' game for my Lisp book of games.
This game uses a binary deduction tree.
The thing is this tree needs to be stored to and loaded from disk. This
is a simple game and I don't want it to depend on external libraries like
object-store. I figure the best way to do this is to store the CLOS tree
as the code I would write to contruct the tree. That way I can just use
read to recontruct it. Never the less the code here looks a bit uggy at
least in (defmethod store ((item tree-node))).
Do you see a better way to do this?

(defmacro awhen (predicate &body body)
  `(let ((it ,predicate))
     (when it
       ,@body)))

;;; Commentary
;;;
;;; The data structure used is a binary deduction tree.
;;; From the tunk on down are questions.
;;; At the leaf nodes are animals.
;;; The deduction treee needs to set up a toxonometry so that
;;; it from a yes or no answers can deduce a unique animal.
;;; If it is not find it after traversing to the leafes
;;; it askes the user to same questions again.
;;; This makes the cursor travers to a leaf node of the tree.
;;; If there is already a animal there it askes for a
;;; question that will distinguish them.

(defclass item ()
  ())

(defclass question (item)
  ((question :accessor text :initarg :text)))

(defclass animal (item)
  ((animal :accessor species :initarg :species)))

(defclass tree ()
  ((root :accessor root :initarg :root :initform nil)
   (cursor :accessor cursor :initform nil)))

(defclass tree-node (item)
  ((item :accessor item :initarg :item)
   (left-child :accessor left-child :initarg :left-child :initform nil)
   (right-child :accessor right-child :initarg :right-child :initform
nil)))

(defmethod initialize-instance :after ((item tree) &key &allow-other-keys)
  (setf (slot-value item 'cursor) (slot-value item 'root)))

(defgeneric store (class stream))

(defmethod store ((item question) stream)
  (print `(make-instance 'question :text ,(text item)) stream))

(defmethod store ((item animal) stream)
  (print `(make-instance 'animal :species ,(species item)) stream))

(defmethod store ((item tree-node) stream)
  (write-string "(make-instance 'tree-node " stream)
  (write-string ":item " stream)
  (store (item item) stream) (values)
  (awhen (left-child item)
    (write-string ":left-child " stream)
    (store it stream))
  (awhen (right-child item)
    (write-string ":right-child " stream)
    (store it stream))
  (write-string ")" stream)
  (values))


Ви мусите увійти перед публікацією повідомлень.
Аби надіслати допис, будь ласка, спочатку приєднайтеся до цієї групи.
Будь ласка, поновіть своє прізвисько на сторінці налаштування передплати перед тим, як надіслати свій допис.
У вас немає права надсилання дописів до цієї групи.
On Nov 6, 11:08 am, John Thingstad <jpth...@online.no> wrote:

> I am  writing a 'guess the animal' game for my Lisp book of games.
> This game uses a binary deduction tree.
> The thing is this tree needs to be stored to and loaded from disk. This
> is a simple game and I don't want it to depend on external libraries like
> object-store. I figure the best way to do this is to store the CLOS tree
> as the code I would write to contruct the tree. That way I can just use
> read to recontruct it.

It might be easier to make the tree out of conses or STRUCTURE-CLASS
instances, both of which can be printed readably?

STRUCTURE-CLASSes even support (single) inheritance and generic
function methods can specialize on them, in case you want to retain
that functionality.

Cheers,
Pillsy


Ви мусите увійти перед публікацією повідомлень.
Аби надіслати допис, будь ласка, спочатку приєднайтеся до цієї групи.
Будь ласка, поновіть своє прізвисько на сторінці налаштування передплати перед тим, як надіслати свій допис.
У вас немає права надсилання дописів до цієї групи.
John Thingstad  
Переглянути профіль   Перекласти вказаною мовою: Перекладено (переглянути оригінал)
 Більше налаштувань 6 Лис, 20:51
Групи новин: comp.lang.lisp
Від: John Thingstad <jpth...@online.no>
Дата: Fri, 06 Nov 2009 12:51:47 -0600
Місцевий час: Пт 6 Лис 2009 20:51
Тема: Re: Is there a better way to store this?
Den Fri, 06 Nov 2009 08:45:50 -0800, skrev Pillsy:

I have not made myself clear. In each of my games I try to use different
tecniques and different approaches. I want to use classes it is just the
way it stores itself I don't like. In particular I don't like write-
string and would have prefered princ.

Basically just one huge line. To pprint it would look nice but would
require the entire structure to be held in RAM and that is just to
wastfull. (Call me old fashioned, but I hate waste.)


Ви мусите увійти перед публікацією повідомлень.
Аби надіслати допис, будь ласка, спочатку приєднайтеся до цієї групи.
Будь ласка, поновіть своє прізвисько на сторінці налаштування передплати перед тим, як надіслати свій допис.
У вас немає права надсилання дописів до цієї групи.
Pascal J. Bourguignon  
Переглянути профіль   Перекласти вказаною мовою: Перекладено (переглянути оригінал)
 Більше налаштувань 6 Лис, 23:10
Групи новин: comp.lang.lisp
Від: p...@informatimago.com (Pascal J. Bourguignon)
Дата: Fri, 06 Nov 2009 22:10:17 +0100
Місцевий час: Пт 6 Лис 2009 23:10
Тема: Re: Is there a better way to store this?

I would avoid using write-string to print something that will be read
back.  Build a sexp, and print it. (Avoid pprint for serialization
since it's slower).

If you have really a lot of data, you could just write one "(" at the
start, and one ")" at the end, and in the middle print several sexps
in a loop (they'll be garbage collected once printed if it is meed).

Of course, this works better if you can linearize the data structure.
Happily, it is easy to put a tree in prefix or suffix form.

--
__Pascal Bourguignon__


Ви мусите увійти перед публікацією повідомлень.
Аби надіслати допис, будь ласка, спочатку приєднайтеся до цієї групи.
Будь ласка, поновіть своє прізвисько на сторінці налаштування передплати перед тим, як надіслати свій допис.
У вас немає права надсилання дописів до цієї групи.
Barry Margolin  
Переглянути профіль   Перекласти вказаною мовою: Перекладено (переглянути оригінал)
 Більше налаштувань 7 Лис, 04:04
Групи новин: comp.lang.lisp
Від: Barry Margolin <bar...@alum.mit.edu>
Дата: Fri, 06 Nov 2009 21:04:48 -0500
Місцевий час: Сб 7 Лис 2009 04:04
Тема: Re: Is there a better way to store this?
In article <87fx8rcqbq....@galatea.local>,
 p...@informatimago.com (Pascal J. Bourguignon) wrote:

Backquote is useful for this:

(defmethod store-expression ((item tree-node))
  `(make-instance 'tree-node                    
                  :item ,(store-expression (item item))
                  ,@(awhen (left-child item)
                      `(:left-child ,(store-expression it)))
                  ,@(awhen (right-child item)
                      `(:right-child ,(store-expression it)))))

(defmethod store ((item tree-node) stream)
  (prin1 (store-expression item) stream))

You might also want to look at PRINT-OBJECT methods.

--
Barry Margolin, bar...@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***


Ви мусите увійти перед публікацією повідомлень.
Аби надіслати допис, будь ласка, спочатку приєднайтеся до цієї групи.
Будь ласка, поновіть своє прізвисько на сторінці налаштування передплати перед тим, як надіслати свій допис.
У вас немає права надсилання дописів до цієї групи.
John Thingstad  <jpth...@online.no> wrote:
+---------------
| I am  writing a 'guess the animal' game for my Lisp book of games.
| This game uses a binary deduction tree.
| The thing is this tree needs to be stored to and loaded from disk.
+---------------

In the "Guess the Animal" game included in the CLLIB subsection of
CLOCC[1], the data is stored as a simple tagged binary tree, that is:

    (LIST* question-string if-yes-subtree if-no-subtree).

with leaves being subtrees that are strings [instead of conses], e.g.:

    (defvar *animals-default-data*
      '("Is it an insect" ("Can it sting" "a bee" . "a roach")
                          "Can it fly" "a duck" . "a penguin"))

That means that the data is *very* easy to write out and read back...

-Rob

[1] http://clocc.cvs.sourceforge.net/*checkout*/clocc/clocc/src/cllib/ani...

-----
Rob Warnock                     <r...@rpw3.org>
627 26th Avenue                 <URL:http://rpw3.org/>
San Mateo, CA 94403             (650)572-2607


Ви мусите увійти перед публікацією повідомлень.
Аби надіслати допис, будь ласка, спочатку приєднайтеся до цієї групи.
Будь ласка, поновіть своє прізвисько на сторінці налаштування передплати перед тим, як надіслати свій допис.
У вас немає права надсилання дописів до цієї групи.
John Thingstad  
Переглянути профіль   Перекласти вказаною мовою: Перекладено (переглянути оригінал)
 Більше налаштувань 7 Лис, 14:44
Групи новин: comp.lang.lisp
Від: John Thingstad <jpth...@online.no>
Дата: Sat, 07 Nov 2009 06:44:34 -0600
Місцевий час: Сб 7 Лис 2009 14:44
Тема: Re: Is there a better way to store this?
Den Fri, 06 Nov 2009 21:27:03 -0600, skrev Rob Warnock:

Agreed. It annoys me though that classes are just about the only thing
where what is printed can't be read back.I never really understood the
assymetry that there is a print-object but no read-object.
Also it's about the only object where structural equality (equalp)
doesn't work.

This is what makes CLOS the problem rather than the solution. Guess all
this customization comes at a price.

I would have liked a simple-class metaclass (alla simple array) where the
two propositions were furfilled. Maybe I'll write one..


Ви мусите увійти перед публікацією повідомлень.
Аби надіслати допис, будь ласка, спочатку приєднайтеся до цієї групи.
Будь ласка, поновіть своє прізвисько на сторінці налаштування передплати перед тим, як надіслати свій допис.
У вас немає права надсилання дописів до цієї групи.
Vassil Nikolov  
Переглянути профіль   Перекласти вказаною мовою: Перекладено (переглянути оригінал)
 Більше налаштувань 7 Лис, 21:50
Групи новин: comp.lang.lisp
Від: Vassil Nikolov <vniko...@pobox.com>
Дата: Sat, 07 Nov 2009 14:50:21 -0500
Місцевий час: Сб 7 Лис 2009 21:50
Тема: Re: Is there a better way to store this?

On Sat, 07 Nov 2009 06:44:34 -0600, John Thingstad <jpth...@online.no> said:

> ...
> there is a print-object but no read-object.

  There is, but the correspondence is not that trivial: PRINT-OBJECT
  is to PRINT & Co. [*] as readtable manipulation is to READ &
  Co. [+].

  _________
  [*] including PRIN1, PRIN1-TO-STRING, WRITE, etc.
  [+] READ, READ-FROM-STRING, READ-PRESERVING-WHITESPACE

  ---Vassil.

--
"Even when the muse is posting on Usenet, Alexander Sergeevich?"


Ви мусите увійти перед публікацією повідомлень.
Аби надіслати допис, будь ласка, спочатку приєднайтеся до цієї групи.
Будь ласка, поновіть своє прізвисько на сторінці налаштування передплати перед тим, як надіслати свій допис.
У вас немає права надсилання дописів до цієї групи.
Thomas F. Burdick  
Переглянути профіль   Перекласти вказаною мовою: Перекладено (переглянути оригінал)
 Більше налаштувань 8 Лис, 14:39
Групи новин: comp.lang.lisp
Від: "Thomas F. Burdick" <tburd...@gmail.com>
Дата: Sun, 8 Nov 2009 04:39:35 -0800 (PST)
Місцевий час: Нд 8 Лис 2009 14:39
Тема: Re: Is there a better way to store this?
On Nov 6, 5:08 pm, John Thingstad <jpth...@online.no> wrote:

> I am  writing a 'guess the animal' game for my Lisp book of games.
> This game uses a binary deduction tree.
> The thing is this tree needs to be stored to and loaded from disk. This
> is a simple game and I don't want it to depend on external libraries like
> object-store. I figure the best way to do this is to store the CLOS tree
> as the code I would write to contruct the tree. That way I can just use
> read to recontruct it. Never the less the code here looks a bit uggy at
> least in (defmethod store ((item tree-node))).
> Do you see a better way to do this?

Do you really need to use defclass here? If you can get by with
structures, they serialize and de-serialize nicely as is.

Ви мусите увійти перед публікацією повідомлень.
Аби надіслати допис, будь ласка, спочатку приєднайтеся до цієї групи.
Будь ласка, поновіть своє прізвисько на сторінці налаштування передплати перед тим, як надіслати свій допис.
У вас немає права надсилання дописів до цієї групи.
Pascal Costanza  
Переглянути профіль   Перекласти вказаною мовою: Перекладено (переглянути оригінал)
 Більше налаштувань 8 Лис, 22:37
Групи новин: comp.lang.lisp
Від: Pascal Costanza <p...@p-cos.net>
Дата: Sun, 08 Nov 2009 21:37:17 +0100
Місцевий час: Нд 8 Лис 2009 22:37
Тема: Re: Is there a better way to store this?

John Thingstad wrote:
> I am  writing a 'guess the animal' game for my Lisp book of games.
> This game uses a binary deduction tree.
> The thing is this tree needs to be stored to and loaded from disk. This
> is a simple game and I don't want it to depend on external libraries like
> object-store. I figure the best way to do this is to store the CLOS tree
> as the code I would write to contruct the tree. That way I can just use
> read to recontruct it. Never the less the code here looks a bit uggy at
> least in (defmethod store ((item tree-node))).
> Do you see a better way to do this?

Are you aware of the following link?

http://web.archive.org/web/20040815123650/http://lecture.pentaside.or...

Pascal

--
My website: http://p-cos.net
Common Lisp Document Repository: http://cdr.eurolisp.org
Closer to MOP & ContextL: http://common-lisp.net/project/closer/


Ви мусите увійти перед публікацією повідомлень.
Аби надіслати допис, будь ласка, спочатку приєднайтеся до цієї групи.
Будь ласка, поновіть своє прізвисько на сторінці налаштування передплати перед тим, як надіслати свій допис.
У вас немає права надсилання дописів до цієї групи.
On Fri, 06 Nov 2009 10:08:46 -0600, John Thingstad <jpth...@online.no> tried to
confuse everyone with this message:

>I am  writing a 'guess the animal' game for my Lisp book of games.
>This game uses a binary deduction tree.
>The thing is this tree needs to be stored to and loaded from disk. This
>is a simple game and I don't want it to depend on external libraries like
>object-store. I figure the best way to do this is to store the CLOS tree
>as the code I would write to contruct the tree.

No, this is a bad idea. If you're going to store objects, using external
libraries such as CL-STORE is the way to go. But in your case, you can easily
store all your data as a list, which is trivial to save or read from disk.

--
|Don't believe this - you're not worthless              ,gr---------.ru
|It's us against millions and we can't take them all... |  ue     il   |
|But we can take them on!                               |     @ma      |
|                       (A Wilhelm Scream - The Rip)    |______________|


Ви мусите увійти перед публікацією повідомлень.
Аби надіслати допис, будь ласка, спочатку приєднайтеся до цієї групи.
Будь ласка, поновіть своє прізвисько на сторінці налаштування передплати перед тим, як надіслати свій допис.
У вас немає права надсилання дописів до цієї групи.
John Thingstad  
Переглянути профіль   Перекласти вказаною мовою: Перекладено (переглянути оригінал)
 Більше налаштувань 9 Лис, 18:32
Групи новин: comp.lang.lisp
Від: John Thingstad <jpth...@online.no>
Дата: Mon, 09 Nov 2009 10:32:46 -0600
Місцевий час: Пн 9 Лис 2009 18:32
Тема: Re: Is there a better way to store this?
The Sun, 08 Nov 2009 04:39:35 -0800, Thomas F. Burdick wrote:

> On Nov 6, 5:08 pm, John Thingstad <jpth...@online.no> wrote:
>> I am  writing a 'guess the animal' game for my Lisp book of games. This
>> game uses a binary deduction tree. The thing is this tree needs to be
>> stored to and loaded from disk. This is a simple game and I don't want
>> it to depend on external libraries like object-store. I figure the best
>> way to do this is to store the CLOS tree as the code I would write to
>> contruct the tree. That way I can just use read to recontruct it. Never
>> the less the code here looks a bit uggy at least in (defmethod store
>> ((item tree-node))). Do you see a better way to do this?

> Do you really need to use defclass here? If you can get by with
> structures, they serialize and de-serialize nicely as is.

Yes, that's what I ended up doing.

(defstruct (animal-name (:type list))
  item
  left-child
  right-child)

(defun questionp (string) (char= (aref string (1- (length string)) #\?))
(defun animalp (string) (not (questionp string))

--
John Thingstad


Ви мусите увійти перед публікацією повідомлень.
Аби надіслати допис, будь ласка, спочатку приєднайтеся до цієї групи.
Будь ласка, поновіть своє прізвисько на сторінці налаштування передплати перед тим, як надіслати свій допис.
У вас немає права надсилання дописів до цієї групи.
Thomas F. Burdick  
Переглянути профіль   Перекласти вказаною мовою: Перекладено (переглянути оригінал)
 Більше налаштувань 9 Лис, 19:02
Групи новин: comp.lang.lisp
Від: Thomas F. Burdick <tburdick+spiced...@gmail.com>
Дата: Mon, 9 Nov 2009 17:02:32 +0000 (UTC)
Місцевий час: Пн 9 Лис 2009 19:02
Тема: Re: Is there a better way to store this?

Why the (:type list)? Structure objects serialize just fine.

Ви мусите увійти перед публікацією повідомлень.
Аби надіслати допис, будь ласка, спочатку приєднайтеся до цієї групи.
Будь ласка, поновіть своє прізвисько на сторінці налаштування передплати перед тим, як надіслати свій допис.
У вас немає права надсилання дописів до цієї групи.
John Thingstad  
Переглянути профіль   Перекласти вказаною мовою: Перекладено (переглянути оригінал)
 Більше налаштувань 9 Лис, 19:58
Групи новин: comp.lang.lisp
Від: John Thingstad <jpth...@online.no>
Дата: Mon, 09 Nov 2009 11:58:32 -0600
Місцевий час: Пн 9 Лис 2009 19:58
Тема: Re: Is there a better way to store this?
The Mon, 09 Nov 2009 17:02:32 +0000, Thomas F. Burdick wrote:

It's easier to debug when you can see the entire tree.
Remeber in the animal guessing game it tries to learn the new animal if
it can't guess it and it is easier to just look at a dump of the list
than to drill through it with the inspector. Oh, and the structure  name
should be node, not animal-name.

--
John Thingstad


Ви мусите увійти перед публікацією повідомлень.
Аби надіслати допис, будь ласка, спочатку приєднайтеся до цієї групи.
Будь ласка, поновіть своє прізвисько на сторінці налаштування передплати перед тим, як надіслати свій допис.
У вас немає права надсилання дописів до цієї групи.
Кінець повідомлень
« Повернутися до обговорень « Новіша тема     Старіша тема »

Створити групу - Групи Google - Домашня сторінка Google - Правила користування послугою - Заява про конфіденційність і нерозголошення інформації
©2009 Google