Re: Prolog list question



On Mar 31, 12:23 am, Nick Wedd <n...@xxxxxxxxxxxxx> wrote:
In message
<5c2ce075-01c2-4eac-abc5-423480851...@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>,
alley <edwardmus...@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    n...@xxxxxxxxxxxxxx Hide quoted text -

- Show quoted text -

Thanks guys for your help.
.



Relevant Pages

  • Re: Prolog list question
    ... i.e. tackling a "base" case and reducing the solution ... first element that, ... begin_with/2 on the tail of that list, ... underscore need not match the same thing. ...
    (comp.lang.prolog)
  • Re: Prolog list question
    ... begin_with/2 on the tail of that list, ... underscore need not match the same thing. ... If the list has a head and a tail, if the head matches the right part, ...
    (comp.lang.prolog)
  • Re: Haskell like (c:cs) syntax
    ... 'head' matches the first element of a list and 'tail' matches the rest? ... could not find this in the Python documentation. ... Which would assign the first element of seq to head, ...
    (comp.lang.python)
  • Re: Python Doc Problem Example: os.path.split
    ... I was working on a program where i needed to split a path into dirname, ... > Split the pathname path into a pair, (head, tail) where tail is the ... > last pathname component and head is everything leading up to that. ... > tail part will never contain a slash; if path ends in a slash, ...
    (comp.unix.programmer)
  • Re: Using a link list over an array.
    ... compile (p->data is a void *) so you have not shown us some key ... int cmp ... head = list_sort; ... list_type *tail; ...
    (comp.lang.c)