Skip to content

Instantly share code, notes, and snippets.

@michaeldperez
Created August 11, 2017 00:39
Show Gist options
  • Save michaeldperez/18ca58004ea011ae53896db1b8e4d287 to your computer and use it in GitHub Desktop.
Save michaeldperez/18ca58004ea011ae53896db1b8e4d287 to your computer and use it in GitHub Desktop.
Determines if a string is a palindrome
-module(palin).
-export([palindrome_check/1]).
rem_punct(String) ->
lists:filter(fun (Ch) ->
not(lists:member(Ch, "\"\'\t\n "))
end, String).
to_small(String) -> lists:map(fun(Ch) ->
case ($A =< Ch andalso Ch =< $Z) of
true -> Ch + 32;
false -> Ch
end
end, String).
palindrome_check(String) ->
Normalize = to_small(rem_punct(String)),
lists:reverse(Normalize) == Normalize.
-module(server).
-import(palin, [palindrome_check/1]).
-export([server/1]).
server(Pid) ->
receive
{check, Msg} ->
IsPalindrome = isPalindrome(palin:palindrome_check(Msg)),
Pid ! {result, io:format("~p is~p a palindrome~n", [Msg, IsPalindrome])},
server(Pid);
stop -> io:format("stopped.~n");
_ ->
io:format("Error: unrecognized message.~n"),
server(Pid)
end.
isPalindrome(Result) ->
case Result of
true -> "";
false -> " not "
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment