Home > Archive > Prolog > October 2004 > Why do i get so many answers?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Why do i get so many answers?
|
|
| Konrad Den Ende 2004-10-08, 8:56 pm |
| 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
---------------------------------------------------
| |
| Stephan Hohe 2004-10-08, 8:56 pm |
| 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/>
| |
| Konrad Den Ende 2004-10-09, 8:56 am |
| 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
---------------------------------------------------
| |
| Konrad Den Ende 2004-10-09, 3:56 pm |
| | |
| Konrad Den Ende 2004-10-09, 3:56 pm |
| |
|
|
|
|