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

Why do i get so many answers?
The objective is to write a clause that will divide a string
into list of strings. More humanly speaking - i'd like Prolog
to find the words in a sentence.

It works decently except for two things. The first is that
it will add an [] to the end of every solution. The other is
that it will have infinitely many solutions obtained by just
adding multiple [] at the end.

I've been staring and grunting for two days but i can't
see where i go wrong. I have drawn hundreds of flow
schedules but every single "proves" that Prolog is wrong.
Any hints/suggestions?

words([], [[]]).
words(A, [C|E]) :-
skipSpaces(A, A2) , getWord(A2, C, D) , words(D, E).

getWord([], [], []).
getWord([A|As], [], [A|As]) :- \+(letter(A)).
getWord([A|As], [A|B], C) :- letter(A) , getWord(As, B, C).

skipSpaces([],[]).
skipSpaces([A1|As], Bs) :- A1 = 32 , skipSpaces(As, Bs).
skipSpaces([A1|As], [A1|As]) :- letter(A1).

letter(C) :- (C >= 0'A , C =< 0'Z) ; (C >= 0'a , C =< 0'z).

--

Kindly
Konrad
---------------------------------------------------
May all spammers die an agonizing death; have no burial places;
their souls be chased by demons in Gehenna from one room to
another for all eternity and more.

Sleep - thing used by ineffective people
as a substitute for coffee

Ambition - a poor excuse for not having
enough sense to be lazy
---------------------------------------------------

Report this thread to moderator Post Follow-up to this message
Old Post
Konrad Den Ende
10-09-04 01:56 AM


Re: Why do i get so many answers?
Konrad Den Ende wrote:
> It works decently except for two things. The first is that
> it will add an [] to the end of every solution. The other is
> that it will have infinitely many solutions obtained by just
> adding multiple [] at the end.

> words([], [[]]).

This always adds an empty word at the end of the string.
(End of the string -> first parameter is [] -> Return a []-word)

> words(A, [C|E]) :-
>     skipSpaces(A, A2) , getWord(A2, C, D) , words(D, E).

This also matches for A==[], if it can find more words in this empty
string...

> getWord([], [], []).

...and it can find more words. This says, an empty string (1st parameter)
contains an empty word (2nd parameter). And a remaining empty list that
matches again on this, so you get a loop here that gives infinitely many
[]-words.

/Stephan

--
Misc stuff: <http://www.tejp.de/>

Report this thread to moderator Post Follow-up to this message
Old Post
Stephan Hohe
10-09-04 01:56 AM


Re: Why do i get so many answers?
OK, i got ridden of the multiple [] by simply asking Prolog
not to let A be a []. Still, i have the issue of getting a []
at the end of *some* solutions. Note - not every, just some
of them. I also have a pretty good idea why it happens but
no compentence to kill it.

I know that the code works nice except for when the input
string ends with a space. Then skipSpaces will clean the
rest of the string but still append the results to the end of
the right solution.

I have tried everything i could have think of but i get either:
1) still the same []-error, or
2) "no" to everything.
That's somewhat too polarized to be acceptable.   :)

The changed part is:
words([], []).
words(A, [C|E]) :- \+(A = []) , skipSpaces(A, A2) ,
getWord(A2, C, D) , words(D, E).
while the rest of the code is as before.

--

Kindly
Konrad
---------------------------------------------------
May all spammers die an agonizing death; have no burial places;
their souls be chased by demons in Gehenna from one room to
another for all eternity and more.

Sleep - thing used by ineffective people
as a substitute for coffee

Ambition - a poor excuse for not having
enough sense to be lazy
---------------------------------------------------

Report this thread to moderator Post Follow-up to this message
Old Post
Konrad Den Ende
10-09-04 01:56 PM


Re: Why do i get so many answers?

Report this thread to moderator Post Follow-up to this message
Old Post
Konrad Den Ende
10-09-04 08:56 PM


Re: Why do i get so many answers?

Report this thread to moderator Post Follow-up to this message
Old Post
Konrad Den Ende
10-09-04 08:56 PM


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:49 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.