31 lines
1.4 KiB
Text
31 lines
1.4 KiB
Text
fun to_locase s = implode ` map (c_downcase) ` explode s
|
|
|
|
fun only_alpha s = implode ` filter (fn x = c_alphabetic x) ` explode s
|
|
|
|
fun is_palin
|
|
( h1 :: t1, h2 :: t2, n = 0 ) = true
|
|
| ( h1 :: t1, h2 :: t2, n ) where ( h1 eql h2 ) = is_palin( t1, t2, n - 1)
|
|
| ( h1 :: t1, h2 :: t2, n ) = false
|
|
| (str s) =
|
|
let
|
|
val es = explode ` to_locase ` only_alpha s;
|
|
val res = rev es;
|
|
val k = (len es) div 2
|
|
in
|
|
is_palin (es, res, k)
|
|
end
|
|
|
|
fun test_is_palin s =
|
|
(print "\""; print s; print "\" is a palindrome: "; print ` is_palin s; println "")
|
|
|
|
fun test (f, arg, res, ok, notok) = if (f arg eql res) then ("'" @ arg @ "' " @ ok) else ("'" @ arg @ "' " @ notok)
|
|
|
|
;
|
|
|
|
println ` test (is_palin, "In girum imus nocte, et consumimur igni", true, "is a palindrome", "is NOT a palindrome");
|
|
println ` test (is_palin, "Madam, I'm Adam.", true, "is a palindrome", "is NOT a palindrome");
|
|
println ` test (is_palin, "salàlas", true, "is a palindrome", "is NOT a palindrome");
|
|
println ` test (is_palin, "radar", true, "is a palindrome", "is NOT a palindrome");
|
|
println ` test (is_palin, "Lagerregal", true, "is a palindrome", "is NOT a palindrome");
|
|
println ` test (is_palin, "Ein Neger mit Gazelle zagt im Regen nie.", true, "is a palindrome", "is NOT a palindrome");
|
|
println ` test (is_palin, "something wrong", true, "is a palindrome", "is NOT a palindrome");
|