Home > Archive > Prolog > May 2004 > How to get rid of assert here?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
How to get rid of assert here?
|
|
| seguso 2004-05-04, 2:59 pm |
| Hello there,
Suppose I have the code
pred(L1, L2):-
forall( (member(M1, L1), member(M2, L2), M2=M1),
myAssert(M)).
myAssert(M):-
assert(foo(M)).
Now I want to change myAssert not to use assert/1:
myAssert(M, LIn, LOut):-
LOut = [foo(M) | LIn].
but how can I change pred/2 accordingly?
Thanks :-)
--
Best Regards,
Maurizio Colucci
Please remove the uppercase letters "S,P,A,M":
seSgPuAsMo.forever@tin.it
| |
| seguso 2004-05-04, 2:59 pm |
| seguso wrote:
> Hello there,
>
> Suppose I have the code
>
> pred(L1, L2):-
> forall( (member(M1, L1), member(M2, L2), M2=M1),
> myAssert(M)).
sorry, that should be myAssert(M1).
>
> myAssert(M):-
> assert(foo(M)).
>
> Now I want to change myAssert not to use assert/1:
>
> myAssert(M, LIn, LOut):-
> LOut = [foo(M) | LIn].
>
> but how can I change pred/2 accordingly?
>
> Thanks :-)
>
--
Best Regards,
Maurizio Colucci
Please remove the uppercase letters "S,P,A,M":
seSgPuAsMo.forever@tin.it
| |
|
| Xref: kermit comp.lang.prolog:18700
[color=darkred]
Hi!
I suggest:
pred(L1, L2) :-
findall(foo(M1), (member(M1, L1), member(M2, L2), M2=M1), FooList).
Dave
| |
| Tom Breton 2004-05-04, 2:59 pm |
| seguso <look@in.signature> writes:
> Hello there,
>
> Suppose I have the code
>
> pred(L1, L2):-
> forall( (member(M1, L1), member(M2, L2), M2=M1),
> myAssert(M)).
>
> myAssert(M):-
> assert(foo(M)).
>
> Now I want to change myAssert not to use assert/1:
>
> myAssert(M, LIn, LOut):-
> LOut = [foo(M) | LIn].
>
> but how can I change pred/2 accordingly?
The basic problem is that now you can't use forall/2, which provides
no way of threading LIn, LOut.
Fortunately, you are just examining lists, so you can rewrite the
formula you passed to forall/2 so that it explicitly walks the lists
while gathering solutions. You won't need to call forall/2.
So your new `pred' would presumably recurse on one list, call
something else to recurse on the other list, which calls myAssert
exactly if a solution was found. And thread LIn, LOut through all
those calls.
And let me channel Paul Tarau for a second and point out that fluents
would solve this problem for arbitrary predicates without the need for
non-trivial rewriting; one can just get the solutions one by one.
--
Tom Breton, calm-eyed visionary
| |
|
| Dave's suggestion is good if you want to keep the same behavior (of course
you want to change pred/2 to pred/3 and return FooList). However, if there
are duplicates, then you can muliply the results. For example,
pred([a,a],[a,a]) ends up with 4 foo(a) clauses asserted.
What we are missing is the specification of the problem. If it is just to
find the intersection of 2 lists, then you have to further ask the question:
can the lists have duplicates, or are they sets?
If they are sets, then you can sort them in order NlogN and compare the two
sorted lists in order N, rather than N squared, rather than comparing each
item of L1 to each item of L2.
W
"Dave" <vd419ll@freemail.hu> wrote in message
news:c70jea$r73$1@namru.matavnet.hu...
>
> Hi!
>
> I suggest:
>
> pred(L1, L2) :-
> findall(foo(M1), (member(M1, L1), member(M2, L2), M2=M1), FooList).
>
> Dave
>
>
>
>
|
|
|
|
|