Re: backtracking in prolog
- From: Chip Eastham <hardmath@xxxxxxxxx>
- Date: Sat, 8 Nov 2008 06:45:06 -0800 (PST)
On Nov 7, 7:21 pm, herlamba <aditya15...@xxxxxxxxx> wrote:
I have a lot of database called: istrue(X), X here is just a char.
so something like:
istrue('a')
istrue('d')
istrue('h')
.......
and the list goes on
I want to create a predicate, which takes two arguments, each which is
a list. The predicate is called foo for example (so foo takes two
list).
what foo does here is to find if there's more than 1 match of istrue
in the database, an example:
foo([istrue], [a,i,e]) will return
true;
true;
true
because foo can match one or more things found in the istrue()
database.
another example
foo([istrue], [d,k,o]) will return
no
because the first character 'd' is not in the istrue database, in
other words there's no istrue('d').
Question is, how can I create such function? So far I've tried and
been able to get one true only
Hi, herlamba:
For future reference, it would be expeditious if
you describe what you tried that didn't work, at
least in part. If nothing else, it helps to
gauge the level of explanation that will be most
helpful to you.
I think I understand the main point you ask about,
though there are a couple of minor inconsistencies
we can skip over for now.
You seem to want the backtracking to continue only
as long as the predicate can be satisfied, ie. to
stop backtracking at the first item in the list
that does not satisfy the given predicate. I say
this because in your second example there was no
backtracking, just stopping after trying istrue('d')
(although istrue('d') was included at the top of
you post in your example code).
So I'd do something like this:
foo( MyPred, [H|_] ) :- not( MyPred(H) ), !, fail.
foo( MyPred, [H|_] ) :- MyPred(H).
foo( MyPred, [_|T] ) :- !, foo( MyPred, T ).
The first clause introduces a cut to force a stop
if an item is encountered that doesn't satisfy the
predicate. Otherwise the attempt to satisfy foo/2
continues backtracking through the list.
When you wrote that "what foo does here is to find if
there's more than 1 match of istrue in the database",
it actually suggested to me a more complicated goal
than what your examples suggest, so if I've perhaps
missed your meaning somewhat.
regards, chip
.
- Follow-Ups:
- Re: backtracking in prolog
- From: equinox
- Re: backtracking in prolog
- Prev by Date: Re: I want a working source code of planning blocks world
- Next by Date: Re: I want a working source code of planning blocks world
- Previous by thread: backtracking in prolog
- Next by thread: Re: backtracking in prolog
- Index(es):
Relevant Pages
|