Palindrome detection: Difference between revisions

Content added Content deleted
(→‎{{header|AppleScript}}: Added demos using just the core language.)
Line 468: Line 468:
{{Out}}
{{Out}}
<pre>true</pre>
<pre>true</pre>
----
===Core language only===
It's not clear if "sequence of characters" means an array thereof or a single piece of text. But the basic method in AppleScript would be:
<lang applescript>on isPalindrome(txt)
set txt to join(txt, "") -- In case the input's a list (array).
return (txt = join(reverse of txt's characters, ""))
end isPalindrome

on join(lst, delim)
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to delim
set txt to lst as text
set AppleScript's text item delimiters to astid
return txt
end join

return isPalindrome("Radar")</lang>

Text comparisons in AppleScript are case-insensitive by default, so:

{{output}}
<lang applescript>true</lang>

If case is to be taken into account, the call to the handler can be enclosed in a 'considering case' control statement.
<lang applescript>considering case
return isPalindrome("Radar")
end considering</lang>

{{output}}
<lang applescript>false</lang>

It's also possible to "ignore" white space, hyphens, and punctuation, which are considered by default. And of course case can be ignored explicitly, if desired, to ensure that this condition's in force during the call to the handler. The attributes can be combined in one statement. So to check for inexact palindromicity (if that's a word):

<lang applescript>ignoring case, white space, hyphens and punctuation
return isPalindrome("Was it a 😀car, or a c😀at-I-saw?")
end ignoring</lang>

{{output}}
<lang applescript>true</lang>


=={{header|Applesoft BASIC}}==
=={{header|Applesoft BASIC}}==