Last active
July 6, 2020 11:37
-
-
Save Joakineee/bd86e6fc9cbbcdd0ed8ba0075c5db5c6 to your computer and use it in GitHub Desktop.
basic palindrome exercise
This file contains hidden or 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(palindrome). | |
-export([server/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) -> | |
Normalise = to_small(rem_punct(String)), | |
lists:reverse(Normalise) == Normalise. | |
server(PID) -> | |
receive | |
{message,String} -> | |
case palindrome_check(String) of | |
true -> PID ! String ++ " is a palindrome", server(PID); | |
false -> PID ! String ++ " isn't a palindrome", server(PID) | |
end; | |
_ -> PID ! stop | |
end. | |
%2> Palindrome = spawn(palindrome,server,[Self]). | |
%<0.82.0> | |
%3> Palindrome ! {message,"test"}, receive X -> X end. | |
%"test isn't a palindrome" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment