Talk:Palindrome detection: Difference between revisions

→‎Prolog: experimenting prolog...
m (→‎Prolog: cut spurious text from a previous writing, when it did not work because of me)
(→‎Prolog: experimenting prolog...)
Line 61:
 
I still have to understand the debug part...; by guessing, I've thought the problem is that when trying the first atom_concat, the unbound atoms are three: X, Mid, and Str2; then I've tried to swap the two atom_concat, since at least the second "knows" Str, then bounds the other two and it makes sense for the second atom_concat the use of X and Str2... Done so, pali('hello') said "no"... but pali('aabbaa') asks True? meaning it can't determine if it is true or false... still needing work... Of course (?) pali('a') says yes because of the second rule. --[[User:ShinTakezou|ShinTakezou]] 22:02, 10 February 2009 (UTC)
 
====What I was able to do until now with Prolog====
 
I sticked to use <code>sub_atom/5</code> to get Head, Tail (one ''char'') and Middle, and produced this code:
 
<lang prolog>pali(Str) :- atom_length(Str, Len), pali(Str, Len).
pali(Str, L) :- L>1, Inlen is L - 2, WLen is L - 1, sub_atom(Str, 1, Inlen, 1, Mid),
sub_atom(Str, 0, 1, WLen, AR), sub_atom(Str, WLen, 1, 0, AL),
AL == AR, pali(Mid, Inlen).
pali(_, 1).
pali(_, 0).</lang>
 
Does it work? Yes... and no. The first oddness is about the last two lines, but I'll write about it later. After I ''consulted'' this code, I was able to do queries, and I did. If I enter a non palindrome sequence, it says no. This is fine and promising. But if I enter a palindrome sequence, it asks True? ... So, it seems we are at the same point as before.
 
The sad fact is the the code for pali(Str, L) was tested interactively and built piece by piece, and each stage seemed to work, and the whole worked too... But I have not found a way to insert and test rules from the interpreter... it seems like it is able just to do queries or clauses...
 
Anyway, if I run
 
<pre>
Str = 'abba', atom_length(Str, L), Inlen is L-2, WLen is L-1,
sub_atom(Str,1,Inlen,1,Mid), sub_atom(Str,0,1,WLen,AR),
sub_atom(Str,WLen,1,0,AL), AL==AR.
</pre>
 
I obtain as output
 
<pre>
AL = a
AR = a
Inlen = 2
L = 4
Mid = bb
Str = abba
WLen = 3
 
yes
</pre>
 
The interactive session just tested that AL and AR are the same; now it would logical to put altogether into a <code>pali(Str, L) :-</code> appending the ''call'' <code>pali(Mid, Inlen)</code>, and I've imagined it should work... But it wasn't able to handle things like pali(<nowiki>''</nowiki>) or pali('a'), because of L-2, so added the check L&gt;1. '''And''', because of some experiments, I believe that the "code" must be differentiated by specifying another argument. The pali/1 is the "access", that "triggers" the ''right'' version of pali/2 according to the second argument.
 
But as said, when it is put into a rules, the code seems to be not able to determine if a word is palindrome, but only if it is not (and this, altogether with the tests made from the command line, is non-sense!).
 
Now, the oddness about last two lines. If I put them before pali(Str, L), then pali(<nowiki>''</nowiki>) and pali('a') ask for Truth. If I put as the code shown above, pali(<nowiki>''</nowiki>) return yes (that is ok), but pali('X') asks for truth (as pali('string with more than one char')). If I swap the two lines, it happens the opposite! pali('a') return yes, pali(<nowiki>''</nowiki>) asks for truth!
 
So order matters, because of a hierarchy of clauses that I tried to understand, but it is too late to be enlightened. Trying blindly other "combinations" brought the same (less or more) results.
 
So now I am convinced I can't do it, and I'm wondering if it exists somewhere a Prolog Guru able to enlighten my poor logic at pro'''log'''-'''ic'''. --[[User:ShinTakezou|ShinTakezou]] 01:24, 11 February 2009 (UTC)