Skip to content

Instantly share code, notes, and snippets.

@emaphis
Last active August 10, 2017 00:30
Show Gist options
  • Select an option

  • Save emaphis/356ee2d96e2cc5febf472487ab6f7999 to your computer and use it in GitHub Desktop.

Select an option

Save emaphis/356ee2d96e2cc5febf472487ab6f7999 to your computer and use it in GitHub Desktop.
Erlang Palindrome Server
%%% @doc Palindrome server
%%% Part1 of exercise
-module(palin).
-export([server/1]).
%% @doc the palindrome server.
server(From) ->
receive
{check, Str} ->
case palindrome_check(Str) of
true ->
From ! {result, "(" ++ Str ++ ") is a palindrome"};
false ->
From ! {result, "(" ++ Str ++ ") is not a palindrome"}
end,
server(From);
_ ->
ok
end.
%% test run:
%% 11> Self = self().
%% <0.31.0>
%% 12> Server = spawn(palin,server,[Self]).
%% <0.57.0>
%% 13> Server ! {check, "Madam"}.
%% {check,"Madam"}
%% 14> Sever ! {check, "dog"}.
%% * 1: variable 'Sever' is unbound
%% 15> Server ! {check, "dog"}.
%% {check,"dog"}
%% 16> Server ! {check, "Madam I\'m Adam"}.
%% {check,"Madam I'm Adam"}
%% 17> Server ! stop.
%% stop
%% 18> flush().
%% Shell got {result,"(Madam) is a palindrome"}
%% Shell got {result,"(dog) is not a palindrome"}
%% Shell got {result,"(Madam I'm Adam) is a palindrome"}
%% ok
%%% Palindrome functions
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.
%%% @doc Palindrome client and server.
%%% Part 2 of exercise.
-module(server).
-export([server/0, client/1]).
%% @doc the palindrome server.
server() ->
receive
{check, Str, From} ->
From ! {result, palindrome_check(Str), Str},
server();
_ ->
ok
end.
%% @doc a client to send palindrome server a message.
client(Pal_server) ->
receive
{send, Str} ->
Pal_server ! {check, Str, self()}, client(Pal_server);
{result, Is_pal, Msg} ->
case Is_pal of
true ->
io:format("(~s) is a palindrome~n",[Msg]);
false ->
io:format("(~s) is not a palindrome~n",[Msg])
end,
client(Pal_server);
_ -> ok
end.
%% example run:
%% 2> Server = spawn(server,server,[]).
%% <0.38.0>
%% 3> Client = spawn(server,client,[Server]).
%% <0.40.0>
%% 4> Client ! {send, "hello"}.
%% (hello) is not a palindrome
%% {send,"hello"}
%% 5> Client ! {send, "Madam"}.
%% (Madam) is a palindrome
%% {send,"Madam"}
%% 6> Server ! stop.
%% stop
%% 7> Client ! stop.
%% stop
%% 8>
%%% Palindrome functions
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.
@Aneesaslim
Copy link
Copy Markdown

Please do write to me on
Aneesa_nda1@hotmail.com
it's vital please
i want to tell you something
Aneesa

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment