What's wrong with my palindrome finding function?

I'm trying to find the palindromes in the dictionary, but my output isn't giving me all of them.

I have this so far

Palindrome[] := DictionaryLookup[y_ /; (y == StringReverse[y])]
Palindrome[]


And my the output just it gives me {a,I}. What's wrong with my input? I don't see it.

Thanks.

• Two underscores after your y should help Oct 22, 2013 at 0:37
• Palindrome[] := DictionaryLookup[y__ /; (y == StringReverse[y])] Oct 22, 2013 at 0:38
• Ah okay thanks. Why did I need two underscores? I read the reference on wolfram, but I'm not sure how it applies here.
– jas
Oct 22, 2013 at 0:51
• A single underscore is for a single character, and double underscore is for one or more. Oct 22, 2013 at 0:55
• So by character, you mean the double underscore is applied to the y and that would give me more than a one letter word?
– jas
Oct 22, 2013 at 0:58

As mentioned in the comments, to search for multi-letter palindromes, two underscores are needed as described in the help for string patterns:

Palindrome[] := DictionaryLookup[y__ /; (y == StringReverse[y])]


Now, Mathematica is probably not good enough to come up with lyrical palindromes.

Mathematica 10.3 introduced the function PalindromeQ, which can be utilized here

palindrome[] := DictionaryLookup[y__ /; PalindromeQ[y]]

palindrome[]

{"a", "aha", "aka", "bib", "bob", "boob", "bub", "CFC", "civic", "dad", "deed", "deified",
"did", "dud", "DVD", "eke", "ere", "eve", "ewe", "eye", "gag", "gig", "huh", "I",
"kayak", "kook", "level", "ma'am", "madam", "mam", "MGM", "minim", "mom", "mum", "nan",
"non", "noon", "nun", "oho", "pap", "peep", "pep", "pip", "poop", "pop", "pup", "radar",
"redder", "refer", "repaper", "reviver", "rotor", "sagas", "sees", "seres", "sexes",
"shahs", "sis", "solos", "SOS", "stats", "stets", "tat", "tenet", "TNT", "toot", "tot",
"tut", "wow", "WWW"}


Unfortunately this is much slower (≈ 10 times slower) than using

DictionaryLookup[y__ /; y === StringReverse[y]]


but can be also used with the option IgnoreCase -> True:

DictionaryLookup[y__ /; PalindromeQ[y, IgnoreCase -> True]]

{"a", "Abba", "Ada", "aha", "aka", "Ana", "Anna", "Ava", "bib", "bob", "Bob", "boob",
"bub", "CFC", "civic", "dad", "deed", "deified", "did", "dud", "DVD", "Ede", "eke",
"ere", "eve", "Eve", "ewe", "eye", "gag", "gig", "Hannah", "huh", "I", "kayak", "kook",
"Laval", "level", "ma'am", "madam", "Malayalam", "mam", "MGM", "minim", "mom", "mum",
"nan", "Nan", "non", "noon", "nun", "oho", "Ono", "Otto", "pap", "peep", "pep", "pip",
"poop", "pop", "pup", "radar", "redder", "refer", "repaper", "reviver", "rotor", "sagas",
"Salas", "sees", "seres", "sexes", "shahs", "sis", "solos", "SOS", "stats", "stets",
"tat", "tenet", "Tet", "Tevet", "TNT", "toot", "tot", "tut", "Tut", "wow", "WWW"}


However, for this using

DictionaryLookup[y__ /; ToLowerCase@y === StringReverse[ToLowerCase@y]]


is much faster.

• interesting. why does the official implementation perform worse than the user-defined one? Oct 16, 2015 at 5:18
• @sunt05 PalindromeQ has additional overheads, especially for strings. Oct 16, 2015 at 5:24
• @sunt05 and it seems, many new functions, at least at first, perform worse, than what could be user-defined in previous versions. One example is ContainsAll and related functions. Oct 16, 2015 at 11:19