Skip to content

Instantly share code, notes, and snippets.

@tecnowilliam
Created June 24, 2020 01:26
Show Gist options
  • Select an option

  • Save tecnowilliam/9132902afd88f847d8ad2633c489a6db to your computer and use it in GitHub Desktop.

Select an option

Save tecnowilliam/9132902afd88f847d8ad2633c489a6db to your computer and use it in GitHub Desktop.
% Compile: c(palin).
% Start the server: Server = spawn(palin,server,[self()]).
% Display the messages in the terminal: flush().
-module(palin).
-author("William Vargas").
-version("1.0").
-export([tests/0, check/1, server/1]).
-spec remove_punctuation(string()) -> list(string()).
remove_punctuation(String) ->
lists:filter(
fun (Ch) -> not(lists:member(Ch," \"\'\t\n,.")) end,
String).
test_remove_punctuation() ->
io:format("test_remove_punctuation> ~p: ~p~n", ["remove_punctuation(\"test,ok\") = testok", remove_punctuation("test,ok") == "testok" ]),
io:format("test_remove_punctuation> ~p: ~p~n", ["remove_punctuation(\"test.ok\") = testok", remove_punctuation("test.ok") == "testok" ]),
io:format("test_remove_punctuation> ~p: ~p~n", ["remove_punctuation(\"test'ok\") = testok", remove_punctuation("test'ok") == "testok" ]),
io:format("test_remove_punctuation> ~p: ~p~n", ["remove_punctuation(\"test\"ok\") = testok", remove_punctuation("test\"ok") == "testok" ]),
io:format("test_remove_punctuation> ~p: ~p~n", ["remove_punctuation(\"test ok\") = testok", remove_punctuation("test ok") == "testok" ]).
-spec to_lowercase(string()) -> list(string()).
to_lowercase(String) ->
lists:map(fun(Ch) ->
case ($A =< Ch andalso Ch =< $Z) of
true -> Ch+32;
false -> Ch
end
end,
String).
test_to_lowercase() ->
io:format("test_to_lowercase> ~p: ~p~n", ["to_lowercase(\"TEST\") = test", to_lowercase("TEST") == "test" ]).
-spec check(string()) -> list(string()).
check(String) ->
Normalise = to_lowercase(remove_punctuation(String)),
lists:reverse(Normalise) == Normalise.
test_check() ->
io:format("test_check> ~p: ~p~n", ["check(\"Madam I\'m Adam.\") = test", check("Madam I\'m Adam.") == true ]).
-spec server(pid()) -> string().
server(Pid) ->
receive
stop ->
io:format("Server stopped ~n");
{check, String} ->
case check(String) of
true ->
io:format("~p is a palindrome.~n",[String]),
Pid ! {result, "\"" ++ String ++ "\" is a palindrome."},
server(Pid);
_ ->
io:format("~p is not a palindrome.~n",[String]),
Pid ! {result, "\"" ++ String ++ "\" is not a palindrome."},
server(Pid)
end;
_ ->
io:format("Server stopped: Invalid message ~n")
end.
test_server() ->
Server = spawn(palin, server, [self()]),
Server ! {check, "Test"},
Server ! {check, "Ma'dam I'm Adam"},
Server ! stop.
-spec tests() -> ok.
tests() ->
test_remove_punctuation(),
test_to_lowercase(),
test_check(),
test_server().
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment