Re: Arrays, functor, and assert/retract
- From: Nick Wedd <nick@xxxxxxxxxxxxx>
- Date: Mon, 19 Nov 2007 21:53:30 +0000
In message <1190940436.628463.172960@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>, pineapple.link@xxxxxxxxx writes
Apparently, doing something as simple as constructing and using an
array in prolog is a hassle and a pain.
It's certainly not what Prolog is good at. If a project involved much work with arrays, I would not use Prolog for it.
However, in an attempt to try
to stick with prolog, vs. throwing it in the garbage and moving on to
another language with less headaches, I have decided to investigate
array construction and manipulation.
Jan says:
<<If you want simple not-too-long arrays of logical variables, you
create
them using functor(Array, Name, Size) and you access using arg(Index,
Array, Element). Works in any Prolog.>>
GNU prolog produces:
<<
| ?- functor(Array, array, 10).
Array = array(_,_,_,_,_,_,_,_,_,_)
yes
What the heck is going on? Is this an array, or a "fact/predicate/
term?" Is there a difference, or not? If there isn't, why? If there
is, why does it look like there isn't?
This is a term. If you assert it (which wouldn't be a sensible thing to do), it also becomes a fact/predicate. If you don't assert it, but create it and pass it from predicate to predicate in the argument list, you can use it to hold an array.
Is this no different than my
simply placing the following fact at the top of my code?
array(_,_,_,_,_,_,_,_,_,_).
Doing that would cause it to be asserted. That does not look like a sensible thing to do.
<< If you want to modify elements beside instantiating variables,
things get more hairy. Plain Prolog can only further instantiate data.
This means you can only write an update_array(Index, Element,
OldArray,
NewArray). Naively this means copying the array.>>
So I take it I cannot change the values inside the so-called "array?"
If you assert it, then indeed you can't change the values in it. But if you create it and pass it around, it can start life being array(_,_,_,_,_,_,_,_,_,_), then its third argument might get instantiated to 19 so that it becomes array(_,_,19,_,_,_,_,_,_,_). Now you can't change the third argument at all, but you can still instantiate the other arguments.
Another question: I assume I could change values in the so-called
"array" (cough) by asserting/retracting a global array.
Assuming you had asserted it: you could, yes.
Now, I
understand that many prologgers would frown upon this. However, I
don't care about that, and I don't care whether or not I'd be
violating some logic programming style. However, I do care (at least,
on a certain level) about efficiency. How expensive is it to assert/
retract?
Generally less efficient than many of the other things Prolog does. How much less efficient would depend on the implementation.
If you really want to keep your array "on the heap", that is, as a fact/predicate, you might instead consider asserting
a(1,_). a(2,_). a(3,_). a(4,_). a(5,_). a(6,_). a(7,_).
a(8,_). a(9,_). a(10,_).
Then you could set the third element to 19 by
retract(a(3,_)), assert(a(3,19)).
and later change it to 0 by
retract(a(3,_)), assert(a(3,0)).
Nick
--
Nick Wedd nick@xxxxxxxxxxxxx
.
- Prev by Date: Re: An even more basic question...
- Next by Date: Re: An even more basic question...
- Previous by thread: Re: An even more basic question...
- Next by thread: equivalentClass
- Index(es):
Relevant Pages
|
|