In situ list manipulation ?
Author Message
In situ list manipulation ?

It is possible to manipulate list in situ in prolog as you do in
Lisp by using the setf function.

For example is possible to translate (setf (cdr '(a b)) 'c) where
the result of the call
'(a. c) is not a copy of the initial argument but the initial list
changed in situ.

In prolog this would look like

?- ((L is [a,b]), setfcdr(L,c)).

C = [a|c]

--
Charles-Andr Brouwers
Paris, France

Wed, 24 Jun 1998 03:00:00 GMT
In situ list manipulation ?

Quote:
>>It is possible to manipulate list in situ in prolog as you do in
>>Lisp by using the setf function.

>>For example is possible to translate (setf (cdr '(a b)) 'c) where
>>the result of the call
>> '(a. c) is not a copy of the initial argument but the initial list
>>changed in situ.

>>In prolog this would look like

>>?- ((L is [a,b]), setfcdr(L,c)).

>>C = [a|c]

as with all things in prolog, if you specify it, you can code it.
if you want to manipulate the tail of a list, represent it.

i refer of course to difference lists: lists that end in a variable
which can be accessed outside the list; usually as the pair:
[Item1,Item2|Tail]-Tail

with this handle on Tail, then we can do things like append
very simply:

dappend(L1-T1,T1-T2,L1-T2).

which works like this

D1= [a,b,c|X]-X, D2 = [d,e,f|Y]-Y, dappend(D1,D2,D3).

D3 = [a,b,c,d,e,f|G1688] - G1688

now if you want tail assignment, you have a handle on it.
for example:

D1= [a,b,c|X]-X, D2 = [d,e,f|Y]-Y, dappend(D1,D2,D3-Z), Z = [].

Z = []
D3 = [a,b,c,d,e,f]

this  is a nice way of doing "append to end of list".

for more on difference lists, see bratko or shaperio's introductory
prolog text.

--
Dr. Tim Menzies                      |

/     \ Dept. Software Development, Monash   | praying; If God talks to you,
\_,-._/ Uni, Caulfield East, Melbourne       | you have schizophrenia."
v  Australia, 3145. +61-3-9903-1033 (ph)| --  Thomas Szasz
+61-3-9903-1077 (fax)                |
"http://www.sd.monash.edu.au/~timm/" |

Thu, 25 Jun 1998 03:00:00 GMT
In situ list manipulation ?

Quote:

>It is possible to manipulate list in situ in prolog as you do in
>Lisp by using the setf function.

>For example is possible to translate (setf (cdr '(a b)) 'c) where
>the result of the call
> '(a. c) is not a copy of the initial argument but the initial list
>changed in situ.

>In prolog this would look like

>?- ((L is [a,b]), setfcdr(L,c)).

>C = [a|c]

I'm curious - is there really some obscure dialect of Prolog that
supports such code?  Certainly none of the three Prolog systems
I have on hand will accept it.

There *is* a builtin setarg/3 that you can use for backtrackable
destructive update in two of those three Prolog systems.  But there is
not portable or standard-conforming way of doing it.

--
Fergus Henderson                WWW: http://www.cs.mu.oz.au/~fjh

Thu, 25 Jun 1998 03:00:00 GMT
In situ list manipulation ?

|>
|> >It is possible to manipulate list in situ in prolog as you do in
|> >Lisp by using the setf function.
|> >
|> I'm curious - is there really some obscure dialect of Prolog that
|> supports such code?  Certainly none of the three Prolog systems
|> I have on hand will accept it.
|>

The Wild LIFE system can do this.  It provides both backtrackable and
nonbacktrackable destructive assignment.  From the manual:

"Wild LIFE provides a clean integration of destructive assignment in a
single-assignment language.  The integration is based on two kinds of terms:
normal and persistent terms.  The former are backtrackable, i.e., they regain
their former values on backtracking.  The latter are nonbacktrackable, i.e.,
changes to them are not undone on backtracking.  Normal and persistent terms may
be matched together.  This results in a flow of information from the persistent
to the normal term, never in the other direction.  Any attempt to modify a
persistent term except through its destructive assignment operation results in
failure."

"The predicate X<-Y overwrites X with Y.  X and Y are normal (backtrackable)
psi-terms."

"The predicate X<<-Y overwrites X with a persistent copy of Y.  Modifications
to X after it has been made persistent are not backtrackable.  If you
backtrack to a point before X is made persistent, then X is restored to its
original (backtrackable) value."

generalization of Prolog, can be found at http://www.isg.sfu.ca/life/.

Regards,

Peter Van Roy

Mon, 21 Sep 1998 03:00:00 GMT

 Page 1 of 1 [ 4 post ]

Relevant Pages