Skip to content

Palindrome detection

editable example

Click the pencil to open this in an editor, change it, and run it in your browser. The same solution is posted on Rosetta Code.

ghul
use IO.Std.write_line
use Ghul.Collections
use Ghul.Pipes
reverse(text: string) -> string =>
text |> reduce("", (reversed, character) => "{character}{reversed}")
is_palindrome(text: string) -> bool => text =~ reverse(text)
is_inexact_palindrome(text: string) -> bool =>
text
|> filter(char.is_letter)
|> map(char.to_lower)
|> reduce("", (letters, character) => "{letters}{character}")
|> is_palindrome()
test(
maybe_palindrome: string,
palindrome_check: string -> bool
) -> string =>
if palindrome_check(maybe_palindrome) then
"\"{maybe_palindrome}\" is a palindrome"
else
"\"{maybe_palindrome}\" is not a palindrome"
fi
write_line(test("racecar", is_palindrome))
write_line(test("hello", is_palindrome))
write_line(test("rotor", is_palindrome))
write_line(test("A man, a plan, a canal: Panama", is_inexact_palindrome))
write_line(test("race car", is_inexact_palindrome))
write_line(test("hello world", is_inexact_palindrome))