Re: Prolog list question
- From: alley <edwardmuscat@xxxxxxxxx>
- Date: Sun, 30 Mar 2008 09:00:42 -0700 (PDT)
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...
.
- Follow-Ups:
- Re: Prolog list question
- From: Chip Eastham
- Re: Prolog list question
- References:
- Prolog list question
- From: alley
- Re: Prolog list question
- From: Chip Eastham
- Prolog list question
- Prev by Date: Re: printing the steps of a derivation
- Next by Date: Re: Prolog list question
- Previous by thread: Re: Prolog list question
- Next by thread: Re: Prolog list question
- Index(es):
Relevant Pages
|
|