Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Prolog list question
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!

Report this thread to moderator Post Follow-up to this message
Old Post
alley
03-30-08 12:22 AM


Re: Prolog list question
On Mar 29, 1:55 pm, alley <edwardmus...@gmail.com> 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

Report this thread to moderator Post Follow-up to this message
Old Post
Chip Eastham
03-30-08 12:22 AM


Re: Prolog list question
On Mar 29, 10:18=A0pm, Chip Eastham <hardm...@gmail.com> wrote:
> On Mar 29, 1:55 pm, alley <edwardmus...@gmail.com> wrote:
>
>
>
>
> 
> 
> 
> 
> 
> 
> 
>
> 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...

Report this thread to moderator Post Follow-up to this message
Old Post
alley
03-31-08 02:04 AM


Re: Prolog list question
On Mar 30, 12:00 pm, alley <edwardmus...@gmail.com> wrote:
> On Mar 29, 10:18 pm, Chip Eastham <hardm...@gmail.com> wrote:
>
>
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
>
> 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

Report this thread to moderator Post Follow-up to this message
Old Post
Chip Eastham
03-31-08 02:04 AM


Re: Prolog list question
On Mar 30, 8:01=A0pm, Chip Eastham <hardm...@gmail.com> wrote:
> On Mar 30, 12:00 pm, alley <edwardmus...@gmail.com> wrote:
>
>
>
>
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
>
> Remember that the version of begin_with which "saves"
> output in a list needs three arguments. =A0If 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. =A0It matches anything, and references to
> underscore need not match the same thing. =A0So I'd say:
>
> begin_with([],_,[]). =A0/* 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).

But then, when the recursion occurs on the tail, its head overrides
the new list's head. I must be missing something here....

Report this thread to moderator Post Follow-up to this message
Old Post
alley
03-31-08 02:05 AM


Re: Prolog list question
In message
<5c2ce075-01c2-4eac-abc5-4234808514b0@y24g2000hsd.googlegroups.com>,
alley <edwardmuscat@gmail.com> writes
>On Mar 30, 8:01_pm, Chip Eastham <hardm...@gmail.com> wrote: 
>
>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@maproom.co.uk

Report this thread to moderator Post Follow-up to this message
Old Post
Nick Wedd
03-31-08 02:05 AM


Re: Prolog list question
On Mar 31, 12:23=A0am, Nick Wedd <n...@maproom.co.uk> wrote:
> In message
> <5c2ce075-01c2-4eac-abc5-423480851...@y24g2000hsd.googlegroups.com>,
> alley <edwardmus...@gmail.com> writes
>
>
>
>
> 
> 
> 
> 
> 
 
> 
> 
s. 
> 
> 
> 
> 
> 
> 
> 
> 
> 
t 
> 
> 
> 
> 
> 
> 
> 
> 
> 
>
> You don't need the cut. =A0And more important, you need to do something
> with Y.
> 
>
> It doesn't "override" (or overwrite?) it. =A0The recursion involves a
> whole new invocation of the predicate, with a whole new H, etc.
>
> You also need to decide what happens if =A0sub_string(H,0,1,_,X) =A0fails.=[/color
]

>
> Nick
> --
> Nick Wedd =A0 =A0n...@maproom.co.uk- Hide quoted text -
>
> - Show quoted text -

Thanks guys for your help.

Report this thread to moderator Post Follow-up to this message
Old Post
alley
04-01-08 02:52 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Prolog archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 05:05 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.