Last active
November 27, 2019 14:11
-
-
Save zmagajna/3b5b8cab5622f2176d777ae05be20925 to your computer and use it in GitHub Desktop.
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
-module(palin). | |
-export([server/1]). | |
% palindrome problem | |
% | |
% palindrome("Madam I\'m Adam.") = true | |
palindrome(Xs) -> palin(nocaps(nopunct(Xs))). | |
nopunct([]) -> []; | |
nopunct([X | Xs]) -> | |
case lists:member(X, "., ;:\t\n'\"") of | |
true -> nopunct(Xs); | |
false -> [X | nopunct(Xs)] | |
end. | |
nocaps([]) -> []; | |
nocaps([X | Xs]) -> [nocap(X) | nocaps(Xs)]. | |
nocap(X) -> | |
case $A =< X andalso X =< $Z of | |
true -> X + 32; | |
false -> X | |
end. | |
% literal palindrome | |
palin(Xs) -> Xs == reverse(Xs). | |
reverse(Xs) -> shunt(Xs, []). | |
shunt([], Ys) -> Ys; | |
shunt([X | Xs], Ys) -> shunt(Xs, [X | Ys]). | |
% server(Pid) -> | |
% end. | |
result(Pid) -> | |
receive | |
{check, S} -> | |
case palindrome(S) of | |
true -> | |
Pid ! {result, "\"" ++ S ++ "\" is a palindrome"}; | |
_false -> | |
Pid ! {result, "\"" ++ S ++ "\" is not a palindrome"}; | |
_stop -> | |
io:format("stopped~n") | |
end | |
end. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment