Google Mail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Is there a better way to store this?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  14 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Follow-up To:
Add Cc | Add Follow-up to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers that you hear
 
John Thingstad  
View profile   Translate to Translated (View Original)
 More options 6 Nov, 16:08
Newsgroups: comp.lang.lisp
From: John Thingstad <jpth...@online.no>
Date: Fri, 06 Nov 2009 10:08:46 -0600
Local: Fri 6 Nov 2009 16:08
Subject: 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))


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Pillsy  
View profile   Translate to Translated (View Original)
 More options 6 Nov, 16:45
Newsgroups: comp.lang.lisp
From: Pillsy <pillsb...@gmail.com>
Date: Fri, 6 Nov 2009 08:45:50 -0800 (PST)
Local: Fri 6 Nov 2009 16:45
Subject: Re: Is there a better way to store this?
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


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
John Thingstad  
View profile   Translate to Translated (View Original)
 More options 6 Nov, 18:51
Newsgroups: comp.lang.lisp
From: John Thingstad <jpth...@online.no>
Date: Fri, 06 Nov 2009 12:51:47 -0600
Local: Fri 6 Nov 2009 18:51
Subject: 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.)


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Pascal J. Bourguignon  
View profile   Translate to Translated (View Original)
 More options 6 Nov, 21:10
Newsgroups: comp.lang.lisp
From: p...@informatimago.com (Pascal J. Bourguignon)
Date: Fri, 06 Nov 2009 22:10:17 +0100
Local: Fri 6 Nov 2009 21:10
Subject: 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__


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Barry Margolin  
View profile   Translate to Translated (View Original)
 More options 7 Nov, 02:04
Newsgroups: comp.lang.lisp
From: Barry Margolin <bar...@alum.mit.edu>
Date: Fri, 06 Nov 2009 21:04:48 -0500
Local: Sat 7 Nov 2009 02:04
Subject: 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 ***


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Rob Warnock  
View profile   Translate to Translated (View Original)
 More options 7 Nov, 03:27
Newsgroups: comp.lang.lisp
From: r...@rpw3.org (Rob Warnock)
Date: Fri, 06 Nov 2009 21:27:03 -0600
Local: Sat 7 Nov 2009 03:27
Subject: Re: Is there a better way to store this?
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


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
John Thingstad  
View profile   Translate to Translated (View Original)
 More options 7 Nov, 12:44
Newsgroups: comp.lang.lisp
From: John Thingstad <jpth...@online.no>
Date: Sat, 07 Nov 2009 06:44:34 -0600
Local: Sat 7 Nov 2009 12:44
Subject: 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..


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Vassil Nikolov  
View profile   Translate to Translated (View Original)
 More options 7 Nov, 19:50
Newsgroups: comp.lang.lisp
From: Vassil Nikolov <vniko...@pobox.com>
Date: Sat, 07 Nov 2009 14:50:21 -0500
Local: Sat 7 Nov 2009 19:50
Subject: 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?"


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Thomas F. Burdick  
View profile   Translate to Translated (View Original)
 More options 8 Nov, 12:39
Newsgroups: comp.lang.lisp
From: "Thomas F. Burdick" <tburd...@gmail.com>
Date: Sun, 8 Nov 2009 04:39:35 -0800 (PST)
Local: Sun 8 Nov 2009 12:39
Subject: 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.

    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Pascal Costanza  
View profile   Translate to Translated (View Original)
 More options 8 Nov, 20:37
Newsgroups: comp.lang.lisp
From: Pascal Costanza <p...@p-cos.net>
Date: Sun, 08 Nov 2009 21:37:17 +0100
Local: Sun 8 Nov 2009 20:37
Subject: 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/


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Timofei Shatrov  
View profile   Translate to Translated (View Original)
 More options 9 Nov, 16:29
Newsgroups: comp.lang.lisp
From: g...@mail.ru (Timofei Shatrov)
Date: Mon, 09 Nov 2009 16:29:41 GMT
Local: Mon 9 Nov 2009 16:29
Subject: Re: Is there a better way to store this?
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)    |______________|


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
John Thingstad  
View profile   Translate to Translated (View Original)
 More options 9 Nov, 16:32
Newsgroups: comp.lang.lisp
From: John Thingstad <jpth...@online.no>
Date: Mon, 09 Nov 2009 10:32:46 -0600
Local: Mon 9 Nov 2009 16:32
Subject: 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


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Thomas F. Burdick  
View profile   Translate to Translated (View Original)
 More options 9 Nov, 17:02
Newsgroups: comp.lang.lisp
From: Thomas F. Burdick <tburdick+spiced...@gmail.com>
Date: Mon, 9 Nov 2009 17:02:32 +0000 (UTC)
Local: Mon 9 Nov 2009 17:02
Subject: Re: Is there a better way to store this?

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

    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
John Thingstad  
View profile   Translate to Translated (View Original)
 More options 9 Nov, 17:58
Newsgroups: comp.lang.lisp
From: John Thingstad <jpth...@online.no>
Date: Mon, 09 Nov 2009 11:58:32 -0600
Local: Mon 9 Nov 2009 17:58
Subject: 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


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google