Re: Prolog list question
- From: Nick Wedd <nick@xxxxxxxxxxxxx>
- Date: Sun, 30 Mar 2008 23:23:28 +0100
In message <5c2ce075-01c2-4eac-abc5-4234808514b0@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>, alley <edwardmuscat@xxxxxxxxx> writes
On Mar 30, 8:01 pm, Chip Eastham <hardm...@xxxxxxxxx> wrote:On Mar 30, 12:00 pm, alley <edwardmus...@xxxxxxxxx> wrote:
> On Mar 29, 10:18 pm, Chip Eastham <hardm...@xxxxxxxxx> wrote:
> > On Mar 29, 1:55 pm, alley <edwardmus...@xxxxxxxxx> wrote:
> > > Hello,
> > > I have created a predicate that takes a list and a character, and
> > > returns the elements beginning with that character:
> > > begin_with([H|T],X) :- sub_string(H,0,1,_,X), !, write(H), nl,
> > > begin_with(T,X).
> > > begin_with([H|T],X) :- begin_with(T,X).
> > > Because of the 'write(H)' clause, Prolog just outputs the elements.
> > > How can I save the returned elements in a list like for example:
> > > begin_with([my,name,is,martin],m,List).
> > > and Prolog returns: List = [my,martin]
> > > true.
> > > Thanks for your help!
> > Think about solving this with a recursive approach,
> > i.e. tackling a "base" case and reducing the solution
> > of any longer list to a shorter one:
> > What result should an empty list produce?
> > If a list is not empty, what should we do with the
> > first element that, together with a call to
> > begin_with/2 on the tail of that list, produces
> > the desired result?
> > regards, chip- Hide quoted text -
> > - Show quoted text -
> I added a base case: begin_with([],X). I've tried several options, but
> can't get a correct implementation to save the output in a list...
Remember that the version of begin_with which "saves"
output in a list needs three arguments. If we start
with an empty list, the output would also be an empty
list, right?
Prolog has a feature called "anonymous variable" given
by underscore. It matches anything, and references to
underscore need not match the same thing. So I'd say:
begin_with([],_,[]). /* base case */
Now how about writing a case (or two) which deal with
a nonempty list, treating the head of the list (first
element) and its tail?
regards, chip- Hide quoted text -
- Show quoted text -
First of all thanks for your help.
OK, so if the list is empty, the output should be an empty list:
begin_with([],X,[]).
If the list has a head and a tail, if the head matches the right part,
it should be the head of the new list: begin_with([H|T],X,[H]) :-
sub_string(H,0,1,_,X), !, begin_with(T,X,Y).
You don't need the cut. And more important, you need to do something with Y.
But then, when the recursion occurs on the tail, its head overrides
the new list's head. I must be missing something here....
It doesn't "override" (or overwrite?) it. The recursion involves a whole new invocation of the predicate, with a whole new H, etc.
You also need to decide what happens if sub_string(H,0,1,_,X) fails.
Nick
--
Nick Wedd nick@xxxxxxxxxxxxx
.
- Follow-Ups:
- Re: Prolog list question
- From: alley
- Re: Prolog list question
- References:
- Prolog list question
- From: alley
- Re: Prolog list question
- From: Chip Eastham
- Re: Prolog list question
- From: alley
- Re: Prolog list question
- From: Chip Eastham
- Re: Prolog list question
- From: alley
- Prolog list question
- Prev by Date: Re: Prolog list question
- Next by Date: Re: fyi Computer Language Benchmarks Game
- Previous by thread: Re: Prolog list question
- Next by thread: Re: Prolog list question
- Index(es):
Relevant Pages
|
|