Re: Prolog list question



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...
.



Relevant Pages

  • Re: [PHP] PHP gives session error on remote server, but not local test machine [SOLVED]
    ... Solving my own issue: ... It turns out that some PHP scripts had an extra carriage return character at the end of the file. ...
    (php.general)
  • Re: Matching 5th and 6th characted
    ... the csv file for those columns whose value is ST for its 5th and 6th character. ... solving this problem. ... Paul Lalli ...
    (perl.beginners)
  • Re: sed, remove last new line
    ... quarkLore wrote... ... solving the problem at hand. ... last new-line character. ... newline, sed provide NO COMMAND to prevent appending a newline to the ...
    (comp.lang.awk)
  • Question
    ... I have the following problem please help me in solving this. ... int i, n=20; ... Only one character in the above needs to be modified such that the character ...
    (comp.lang.cpp)
  • Question
    ... I have the following problem please help me in solving this. ... int i, n=20; ... Only one character in the above needs to be modified such that the character ...
    (comp.lang.cpp)