Created
February 9, 2016 04:04
-
-
Save vysakh0/4b76c1976106b679d910 to your computer and use it in GitHub Desktop.
A simple palindrome without using any inbuilt functions like `String.reverse` or `String.length`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Palindrome do | |
def check(<< first :: binary-size(1), rest :: binary >> = actual) do | |
check(first, rest, actual) | |
end | |
def check(newstr, << next_letter :: binary-size(1), rest :: binary >>, actual) do | |
check(next_letter <> newstr, rest, actual) | |
end | |
def check(reverse, <<>>, actual), do: reverse === actual | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule PalindromeTest do | |
use ExUnit.Case | |
test "check if palindrome" do | |
assert Palindrome.check("elixir") === false | |
assert Palindrome.check("madam") === true | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Bottom case
clojure-version
haskell-version
elixir-version