Code Comments
Programming Forum and web based access to our favorite programming groups.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!
Post Follow-up to this messageOn 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
Post Follow-up to this messageOn 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...
Post Follow-up to this messageOn 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
Post Follow-up to this messageOn 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....
Post Follow-up to this messageIn 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
Post Follow-up to this messageOn 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.
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.