Article View: comp.lang.prolog
Article #1150Re: Arrays in Prolog
From: Jose Alberto Fernandez R
Date: Fri, 31 Aug 1990 18:47
Date: Fri, 31 Aug 1990 18:47
58 lines
3019 bytes
3019 bytes
In article <9668@bunny.GTE.COM> tomf@GTE.COM (Tom Fawcett) writes:
The "obvious" way of representing these is to use a predicate like
"boardn(square,contents)", eg "board73(a3,blank)", where a3 is a
square
designator. Because Quintus Prolog indexes on the functor and
first argument,
I can access and update a given square of a given board in constant
time.
However, these take up more space than a 64-element vector, and
it's still too
slow (every modification must involve a retract and an assert,
which is much
slower than a destructive array assignment). Also, I often want to
make one
board be a slight modification of another, which involves copying
the contents
of one board to another, and then making the change. This is also
relatively
slow.
Your problem is that you are trying to express "time" in your program,
without expressing it.
1) Let us look at your predicate "boardn(square, contents)", you are
trying to express with "board73(a3,blank)" that at some point in time
board73 has a blank in position a3. Actually, you probably want to
express information about only one board per game. So, why you dont
say what you really want: "board(square, time, contents)" now you now
at at "time" some "content" was in position "square".
2) You don't need to represent "blanks". If the position X is not true
in the database, for a given point in time, then assume is blank.
3) You can compute the value of a square, with the following rule:
value(Square, Time, Contents):-
( (board(Square, Time1, Contents1),Time1 < Time) =>
\+ moved(Square,Time1)
; Contents = blank ).
where "moved(Square, Time)" indicates that whatever it was in position
Square at Time, has been moved. Therefore now there is a blank.
Note that for this code to work you should do asserta instead of assert.
This means that you never retract information, only assert
information, unless you want to do some garbage collection every n
moves, that shouldn't be difficult to implement.
4) With this approach you are recording much less information than the
entier board every time.
Jose Alberto.
--
:/ \ Jose Alberto Fernandez R | INTERNET: alberto@cs.umd.edu
:| o o | Dept. of Computer Sc. | BITNET: alberto@cs.umd.edu
:| ^ | University of Maryland | UUCP: {...}!mimsy!alberto
:\ \_/ / College Park, MD 20742 |
Technical Details
Message-ID:
<ALBERTO.90Aug31144731@yamuna.cs.umd.edu>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!wuarchive!uwm.edu!linac!midway!mimsy!mimsy.umd.edu!albertoReferences:
<90239.175243SCHMIED@DB0TUI11.BITNET> <9653@bunny.GTE.COM> <3629@goanna.cs.rmit.oz.au> <9668@bunny.GTE.COM>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!wuarchive!uwm.edu!linac!midway!mimsy!mimsy.umd.edu!alberto From: alberto@cs.umd.edu (Jose Alberto Fernandez R) Newsgroups: comp.lang.prolog Subject: Re: Arrays in Prolog Message-ID: <ALBERTO.90Aug31144731@yamuna.cs.umd.edu> Date: 31 Aug 90 18:47:31 GMT References: <90239.175243SCHMIED@DB0TUI11.BITNET> <9653@bunny.GTE.COM> <3629@goanna.cs.rmit.oz.au> <9668@bunny.GTE.COM> Sender: news@mimsy.umd.edu Organization: University of Maryland Computer Sc. Lines: 58 In-reply-to: tomf@GTE.COM's message of 30 Aug 90 16:21:27 GMT