Talk:Palindrome detection

From Rosetta Code
Revision as of 15:54, 10 February 2009 by rosettacode>Mwn3d (→‎Prolog: Should use strings)

Spaces and punctuation

The task specification states both:"The function must not ignore spaces and punctuations" and later: "you must make it all the same case and strip spaces".

So ignore or strip spaces - what should be done? --Paddy3118 20:56, 5 December 2008 (UTC)

I think what is meant is that the function itself shouldn't ignore spaces or punctuations, but if using the given example, the test code must remove spaces and convert to either uppercase or lowercase, because otherwise the example isn't a character-wise palindrome. I think it would make more sense to use some example which already is a character-wise palindrome by itself, say the German word "nennen" (to name). --Ce 09:42, 6 December 2008 (UTC)
It is what I've meant... As example of palindrome I used "In girum imus ..." just because it is the longest I know by heart: the "ignore or strip spaces" refers to that example as I wrote there; looking at the code it is clear what I've meant. According to me a function that tests the palindromicity of a "arbitrary" sequence is more general than one that "ignores" by design some characters; you can use the function you wrote to test palindromicity on "In girum imus nocte ...", provided that you strip spaces and make the case of all letters the same... outside the function, e.g. is_palindrome(stripchars(string)), and write stripchars according to any need. In the codes, I did it by hand coding the test string as "ingirumimusnocte...", that is less readable, but in this way I had not to write a stripchars function just to test! --ShinTakezou 00:44, 7 December 2008 (UTC)
And hey, my english is bad, but not so bad: I've forgot it, but I wrote To do your test with it, you must make it all the same case and strip spaces., where the "it" refers to the "In girum imus..." palindrome, and to do your test with it means ... :D --ShinTakezou 00:44, 7 December 2008 (UTC)
Hi ShinTakezou, please accept my apologies if you thought I was gratuitously slighting your English - that was not my intention. I was merely pointing out what appeared odd to me in reading your task. I could figure out what is needed but was hoping for the task description to be clarified to remove any doubt. When a task accumulates several implementations it can be difficult to work out which is the first implementation so if implementations differ, it is hard to refer to the first implementation for a definitive answer. --Paddy3118 01:12, 7 December 2008 (UTC)
Got your point. Chances there are that my main contribute is in the C language :) --ShinTakezou 00:25, 8 December 2008 (UTC)

Haskell recursive solution note

I suppose the Haskell recursive code can be written a lot better, but I don't know how. --ShinTakezou 14:08, 5 December 2008 (UTC)

Does this look better to you?
is_palindrome_r x | length x <= 1 = True
                  | head x == last x = is_palindrome_r . tail. init $ x
                  | otherwise = False
--Gaaijz 14:37, 5 December 2008 (UTC)
Yes, if it works, why don't you put it in? I've tested it, it works, put it in instead of mine ;) --ShinTakezou 19:09, 5 December 2008 (UTC)

The Python recursive example isn't testing that the string is a palindrome. It seems to actually be a test for whether the object tested supports len() and slicing. Drea 16:56, 5 December 2008 (UTC)

Why? I am still learning Python... even though there's no type check (right?) I suppose the normal use of such a function is on strings or arrays, both supporting len and slicing; maybe it makes sense to be able to test palindromicity also on other objects. Anyway it worked rather well when tested ;) --ShinTakezou 19:09, 5 December 2008 (UTC)
It looks like the Perl example doesn't check for character equality either. --Mwn3d 16:59, 5 December 2008 (UTC)
Oh my ... yes, thanks (the same as Python... can't remember if I simply forgot or were cut... fixing --ShinTakezou 19:09, 5 December 2008 (UTC)
I've fixed the Python example. I don't know any Perl though, so I'll leave that for someone else. Drea 17:03, 5 December 2008 (UTC)
Oh my ... yes, thanks :) --ShinTakezou 19:09, 5 December 2008 (UTC)
No problem :o) Drea 19:29, 5 December 2008 (UTC)

Prolog

I've tested it with gprolog... it does not work... nor I know exactly what is not working; but it seems quite interesting; I will spend a little bit of time taking a look at prolog (there's something that reminds me erlang, maybe consult/1 or notation like this...)

I think the problem is with the string functions I used. I can't figure out how to get the right library loaded. I found the functions here...can anyone help? --Mwn3d 03:32, 10 February 2009 (UTC)

I've tried to run several examples of prolog, not always with success. There's something still obscure (beyond the language itself, that seems not suitable for some kind of out-of-logic task; it should share something with functional programming, but I've failed using my poor knowledge of e.g. Haskell; nor I've found well-done full manuals, and tutorials stress the logic ability of the language...) --ShinTakezou 15:37, 10 February 2009 (UTC)

Found this: palindrome?, but likely it is not suitable for the wiki because of the license... Despite this, I am not sure it works... when I feed it with a palindrome string or list, it "asks" me True? :D

| ?- pal([a,b,c]).

no
| ?- pal([a,b,c,b,a]).

true ? 

yes

Like if the clause were undetermined (and in fact pressing ; for the next solution, gives no, not yes!)... maybe I should read deeper the page... --ShinTakezou 15:50, 10 February 2009 (UTC)

I don't think this solution is very good anyway because it uses a list of characters rather than a string. I'd rather see one using string library functions. --Mwn3d 15:54, 10 February 2009 (UTC)